Reputation: 3159
My app has several viewControllers and for some of them, I would like to use a different navigationbar background image when they are pushed onto the navigation stack.
For example, when my mainViewController loads up, I have this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Setting toolbar for iOS 5+");
UIImage * navbarImage = [UIImage imageNamed:@"navbar01.png"];
[[UINavigationBar appearance] setBackgroundImage:navbarImage forBarMetrics:UIBarMetricsDefault];
}
Likewise, when my other viewController is pushed onto the navigation stack, I am using the same code as above, except using a different UIImage (navbar02.png).
However, the navigation bar doesn't change from the first image that I set. Is there any way to change the UINavigationBar background image when a new view appears?
Thank you!
Upvotes: 2
Views: 1362
Reputation: 3159
Here is the link for similar question: UINavigationBar setBackgroundImage: forBarMetrics: Not Working
The answer is to use this:
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
rather than the code you have used.
Upvotes: 2
Reputation: 5164
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
// NSLog(@"%f",version);
if (version >= 5.0) {
UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else
{
// UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"NavBar" ofType:@"png"];
[self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
}
its working successfully for me ... maybe it will help you.
Upvotes: 1