Change navigation bar Background image for each navigation

I am working on an iPhone. I added a navigation bar Background image

With interface: -

@interface UINavigationBar (backgroundImageWithTitle)

And the method: -

- (void)drawRect:(CGRect)rect

Using this method, background images in the navigation bar are set once.

I want to call it from different .m files to assign different images to the panel.

How can this be implemented?

Thanks in advance.

+7
source share
6 answers

CustomNavigation.h

  #import <Foundation/Foundation.h> @interface UINavigationBar (UINavigationBarCustomDraw){ } @end 

CustomNavigation.m

  @implementation UINavigationBar (UINavigationBarCustomDraw) - (void) drawRect:(CGRect)rect { [self setTintColor:[UIColor colorWithRed:0.5f green: 0.5f blue:0 alpha:1]]; if ([self.topItem.title length] > 0) { if ([self.topItem.title isEqualToString:@"First"]) { [[UIImage imageNamed:@"First.png"] drawInRect:rect]; } else if ([self.topItem.title isEqualToString:@"Second"]) { [[UIImage imageNamed:@"Second.png"] drawInRect:rect]; } CGRect frame = CGRectMake(0, 0, 320, 44); UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; [label setBackgroundColor:[UIColor clearColor]]; label.font = [UIFont boldSystemFontOfSize: 20.0]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.text = self.topItem.title; self.topItem.titleView = label; } else { [[UIImage imageNamed:@"wood.png"] drawInRect:rect]; self.topItem.titleView = [[[UIView alloc] init] autorelease]; } } @end 

if u wants First.png to set the navigationBar background image to FirstViewController, then

in ur FirstViewController.m

  -(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.title=@ "First"; [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)]; } 

if u wants Second.png to set the background image of navigationBar to SecondViewController, then

in ur SecondViewController.m

  -(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.title=@ "Second"; [self.navigationController.navigationBar drawRect:CGRectMake(0, 0, 320, 480)]; } 
+9
source

On iOS 5, this will not work, but the good news is that there is an easy way to do this.

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"name"] forBarMetrics:UIBarMetricsDefault]; 

NOTE. This will only work on iOS 5 and above, so make sure you check the iOS version if you want it to be backward compatible.

+2
source

Or another option, so the code only runs with iOS 5

 if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0 { [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone]; } 
+1
source

You can try this way

 if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0 { //[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 40)]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone]; [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; } 

otherwise you can add the previous code ie setBackgroundImage ....

+1
source

Usage (SDK 3.2.5.) . In one of my applications, I needed a navigationBar with a custom image in both the landscape and the portrait .

What I've done:

 @implementation UINavigationBar (BackgroundImage) - (void)drawRect:(CGRect)rect { UIImage *image; if(self.frame.size.width == 320) //for iPhone - portrait will have 320 pix width. { image = [UIImage imageNamed: @"portraitNavigationBar.png"]; } else { image = [UIImage imageNamed: @"landscapeNavigationBar.png"]; } [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; } @end 

Fortunately, my application supports rotation - so when you rotate - the navigation bar automatically redraws itself.

But for manual redrawing - you can also call:

 [navigationController.navigationBar setNeedsDisplay]; 
+1
source

I tried this

[[Appearance of UINavigationBar] setBackgroundImage: [UIImage imageNamed: @ "name"] forBarMetrics: UIBarMetricsDefault];

It works great for iOS 5.0, but has hit iOS 4.3 or lower.

Do you have conditional ie installed for

iOS 5

[[Appearance of UINavigationBar] setBackgroundImage: [UIImage imageNamed: @ "name"] forBarMetrics: UIBarMetricsDefault];

still

[[Appearance of UINavigationBar] setBackgroundImage: [UIImage imageNamed: @ "name"]];

but still there is a problem that it shows the line below the image.

Or you can do it this way

 self.navigationItem.hidesBackButton = YES; UINavigationBar *navBar = self.navigationController.navigationBar; UIImage *image = [UIImage imageNamed:@"image.png"]; UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)]; imgView.image = image; [tempView addSubview:imgView]; [navBar clearBackgroundImage]; [navBar addSubview:tempView]; [tempView release]; 
0
source

All Articles