I've been looking around for a way to change the background image of my NavigationBar
and control the appearance of my NavigationBar
as the user navigates the app.
I understand that the accepted approach for changing the background image is :
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
However, that changes the appearance of the NavigationBar
throughout the whole app. How can I change the background image of the NavBar
as the user navigates from one view to the next?
Thanks in advance for your help!
I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.
isn't it possible to use an image view (add) on the navigation bar?
You can use this code any where in your application(Call the method in viewWillAppeare:) by calling the method to change the navigation bar image. If You call the method in didFinishLanch means the navigation bar image is set to the whole app.
You need to set some state somewhere about the current page or currently appropriate image to use, probably in each of your
viewWillAppear:
methods. Then modify yourdrawRect:
function above to reference that state.To cause the bar to be redrawn, call
[myNavigationBar setNeedsDisplay]
when you update the state. This will causedrawRect
to be invoked.I'd really recommend reading a tutorial from Sebastian Celis, it really helped me - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/