I have an iPhone application using UINavigationController and would like to customize the elements with custom background images. I was able to do this for the UINavigationController's UINavigationBar pretty easily using Objective-C categories as below:
http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html
I’d like to do the same for the UINavigationController's UIToolbar, but the same approach doesn’t seem to work (although I have absolutely no idea why not.) I’ve looked around and people seem to suggest subclassing UIToolbar, but this isn’t possible for the UINavigationController's toolbar, which is a read-only UIToolbar. I want to use the UINavigationController's toolbar instead of just making a subview toolbar because I’m using the slide-in setToolbarHidden animation.
Anyone have any idea if it’s possible to apply a background image to this UINavigationController toolbar (most likely by somehow overriding the drawRect method)?
You need to subclass instead of creating a category.
I’m not too sure why a category works for
UINavigationBarbut notUIToolbar, but subclassing works for both of them and I think it’s the more standard way of customizing stuff like that.So to subclass
UIToolbar, create a class calledMyToolbar(or something similar) and put this in the .h:And this in the the .m file:
From there you’ve got to tell the Navigation Controller to use
MyToolbarinstead ofUIToolbar. Easiest way I’ve found to do that is to select your Navigation Controller in Interface Builder, then in the Attributes Inspector check the ‘Shows Toolbar’ box. The toolbar should show up in theNavigationControllerwindow. Click on it and then in the Identity Inspector change the class toMyToolbar.