0

I have the following code in viewWillAppear in my view controller.

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    UIImage *image = [UIImage imageNamed:@"navbar.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.navigationController.navigationBar addSubview:imageView];
}

How do I clear the navigation bar background image that is already there before adding a new nav bar image as in the code above?

Also, how can I set a different navigation bar background image for each different controller the most efficient way possible? thanks!

Kermit the Frog
  • 3,949
  • 7
  • 31
  • 39
  • use UIAppearance for setting different navbar styles. see http://stackoverflow.com/questions/8736171/how-to-customize-uinavigationbar-in-ios5?lq=1 – Daij-Djan Nov 25 '12 at 22:59

1 Answers1

1

In your viewWillAppear method, use the following call to set the background image of the navigation bar.

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];

If your app supports landscape as well, you'll also need to set the background image for the landscape navigation bar with a different height:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
meim
  • 740
  • 4
  • 7
  • I have a question - how do we prevent the back button from being animated when people click back? Right now the back button slides to the center when clicked. How can we stop that? – Kermit the Frog Nov 25 '12 at 23:27
  • in viewWillDisappear method, set the back button to nil: self.navigationItem.leftBarButtonItem = nil – meim Nov 25 '12 at 23:34