I want to add an image background to my navigation bar.
Is it right?
//set custom background image UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavigationBackground.png"]]; [self.navigationBar insertSubview:backgroundView atIndex:0]; [backgroundView release];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With