How to add background image on iphone Navigation bar?
Solution 1:
Here's the code from the link @luvieere mentioned.
Paste this code into to the rootview controller just above
@implementation rootviewController
@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
As of iOS 5, there is an official way to do this. (see iOS Developer Library)
// someplace where you create the UINavigationController
if ([navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:@"NavigationBar.png"];
[navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
But still, retain the old code for backward compatibility unless you really want to ditch iOS 4 and below.
Solution 2:
Your code alone won't do it, you'll have to write a category in order for it to work. There are two approaches regarding the way you should do it: the first one involves making the image a subview of your UINavigationBar
and re-bringing it to front in each of your UIViewController
's viewDidAppear
method. This however has been reported having some issues with covering the right UIBarButtonItem
. The other method involves overriding
- (void)drawRect:(CGRect)rect
and drawing the image there. Both of these are extensively covered in this blog discussion..