Last Updated: February 25, 2016
·
1.774K
· blakelucchesi

Change UINavigationBar background image.

When changing the background image of a UINavigationBar it's important to do it during viewWillAppear or viewWillDisappear. I tried setting it during the initialization of the new ViewController I was pushing onto the stack and it didn't work.

- (void)viewWillAppear:(BOOL)animated {
  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationImage.png"] forBarMetrics:UIBarMetricsDefault];
}

2 Responses
Add your response

Or you could use the Appearance protocol :
UINavigationBar *appearance =[UINavigationBar appearance]; [appearance setBackgroundImage:[UIImage imageNamed:@"NavigationImage.png"] forBarMetrics:UIBarMetricsDefault]; </code>

This way you can customise your navigation bar in -viewDidLoad</code> method, or in ApplicationDelegate.m</code> or even better you could create a theme system that allows you to easily maintain your design and reuse it in other applications.

over 1 year ago ·

@alexcristea, it's better be done from AppDelegate, Apple suggests to setup UIAppearance properties on the early stage of application lifecycle.

over 1 year ago ·