Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add background image on iphone Navigation bar?

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]; 
like image 339
Mc.Lover Avatar asked Nov 07 '09 08:11

Mc.Lover


1 Answers

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.

like image 141
iwat Avatar answered Sep 22 '22 14:09

iwat