Most iPhone development projects work just fine without the need to customize the appearance of the navigationBar. But once in a while you probably would like to change the background of the navigationBar.
Your RootViewController has acces to a NavigationItem, which has a property titleView. This view holds the title of your current active view in the navigationControllers stack. However setting the titleView to one of your customViews, will not take up the whole background of the NavigationBar. This is because the NavigationBar consists of not only the titleView, but also has room for the default back button an perhaps another UIButton at the right.
If you want a custom background for your navigationBar, and still want to maintain the ability place custom labels on top of it, without having to worry about views disappearing behind the background you have set, the following snippet will help you to do just that.
Open your application MyAppDelegate.m and place the following implementation above the implementation of our MyAppDelegate.
//Overide the drawing of the background UINavigationBar
@implementation UINavigationBar (image)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"my_navigation_bar_bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
What it does is, whenever the application tries to draw the navigationBar, it uses the image “my_navigation_bar_bg.png” as your source to draw the containing rect of the navigationBar. So all other UIElements are draw on top of this image. The default height of the navigationBar is 44 pixels. So an image of 320×44 will fill up the total space behind all navigationItems in this application.