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];
}
Written by Blake Lucchesi
Related protips
2 Responses
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.
@alexcristea, it's better be done from AppDelegate, Apple suggests to setup UIAppearance properties on the early stage of application lifecycle.