I would like to set an image background to the navigation bar on my iphone app. Most solutions suggest using drawRect in a category like:
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
However, Apple does not recommend this. Any other suggestion?
Thx for helping,
Stephane
Apple strongly advise us to use subclasses rather than categories (WWDC 2011, Session 123).
Create a subclass which implements the
drawRect:method and set the class of your navigation bar to your custom class:Class switch at runtime:
You should also avoid using
[UIImage imageNamed:...]each time indrawRect:as it might have an impact on performance (for animations). Cache it in an ivar:(and release it in dealloc)
Note: As iOS 5 is still under NDA, I can’t mention how you could easily add a background image. Check out the docs for UINavigationBar.