noahd
noahd

Reputation: 862

iphone - uinavigationbar custom image change as new view controller are popped off the stack

I currently have a uinavigationbar in my app with a custom image for the background. I've implemented this using the category technique. Something like this:

@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

I guess it is also possible to method swizzling but from everything I've read this isn't a great approach as UIKit changes will break my code. The problem I have with approach this is changing the background image when new view controllers are popped off the stack. I seem to be able to modify the image when they are pushed but when I pop the view controller I am not able to change the image back. Is there a way I can listen for the user taps the back button on the navigationcontroller and trigger an image change at that point? Also curious as to whether this kind of practice is discouraged by Apple.

Upvotes: 0

Views: 1015

Answers (2)

Manoj Kumar
Manoj Kumar

Reputation: 370

you could try this way

create a global constant variable.

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image =nil;
if(CONSTANT==1)
    image= [UIImage imageNamed: @"NavigationBar.png"];
else
    image= [UIImage imageNamed: @"newbar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

in UIViewController

-(void)viewWillAppear:(BOOL)animated{
   CONSTANT=2;//CONSTANT=1
   [self.navigationController.navigationbar setNeedsDisplay];
}

Upvotes: 0

Pascalius
Pascalius

Reputation: 14659

You could try to use this method to set the background image programmatically. Link to Apple Docs

willShowViewController:animated

Upvotes: 1

Related Questions