1

I have read this and change UINavigationBar's background:

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"top-with-logo.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

you see, I add a image named top-with-logo.png, and I want to change the background view by adding an image named top-without-logo.png. But the code above will change the background forever and i cannont change any more. Do you know how?

Thank you very much.

Community
  • 1
  • 1
Smeegol
  • 2,014
  • 4
  • 29
  • 44

2 Answers2

1

I'm not sure I understand what you mean, but you can place a Boolean ivar on your app's delegate. then in the function you load top-with-logo.png if it's true/false and load top-without-logo.png if it's the other value. then, when you want to change the logo, just change the Boolean's value and call setNeedsDisplay on the navigationBar.

if you use xib files, you can also subclass UINavigationBar, and instead of using a catagory you override the drawRect function and place the variable as an instance variable as a subclass, I believe it is cleaner, but you can only set that up in Interface builder ( otherwise there's no way to tell a UINavigationController to use a different class for the navigationBar property )

Niv
  • 2,294
  • 5
  • 29
  • 41
  • Just wanted to add that you actually _can_ set the navigation bar's class in code using object_setClass(id object, Class class) defined in Objc runtime. – mjisrawi Aug 08 '11 at 10:26
0

You can use,

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55