views:

67

answers:

2

Hello. I've 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've been looking around and it appears the accepted approach for changing the background image is this...

@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!

A: 

isn't it possible to use an image view (add) on the navigation bar?

karim
+1  A: 

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 your drawRect: function above to reference that state.

To cause the bar to be redrawn, call [myNavigationBar setNeedsDisplay] when you update the state. This will cause drawRect to be invoked.

Seamus Campbell
thanks, seamus, but it looks like the drawRect routine only gets called the first time the navigation bar is drawn. as the user navigates deeper into the navigation hierarchy, the drawRect routine doesn't get called. is there a way to force the drawRect routine to get called every time i change views?
BeachRunnerJoe
See edit above.
Seamus Campbell
thanks, seamus, that worked great!
BeachRunnerJoe