3

I'm using de following code for placing an image on the background of the UINavigation Bar:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_background_login.png"] forBarMetrics:UIBarMetricsDefault];

In iOS 7 works like a charm but when I try it in the same simulator device but with iOS 8 the background image doesn't show.

There is some new code for iOS 8? I can't find anything on the net.


UPDATE:

I've discovered one of the most weird behaviors I've ever seen. When I show and later dismiss a modalView, then the Background Image of the UINavigationBar appears.

For more information: I'm loading the image in the viewDidLoad.

Any thoughs?

miquelweb
  • 43
  • 1
  • 6

2 Answers2

5

Try this it worked for me:-

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_background_login.png"] forBarMetrics:UIBarMetricsDefault];
Rajat
  • 1,043
  • 12
  • 23
  • It worked perfect!! I can believe I didn't tried that. – miquelweb Sep 18 '14 at 14:49
  • 1
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_background_login.png"] forBarMetrics:UIBarMetricsDefault]; write this in your app delegate "didFinishLaunchingWithOptions" method it will set the nav bar background image app wide. – Rajat Sep 18 '14 at 14:54
  • For a better solution and different screen sizes see https://stackoverflow.com/questions/43602848/ – jn-se Aug 05 '17 at 19:40
2

Using [UINavigationBar appearance] to set the UINavigationBar background globally is correct. But please note that the appearance affects only the future instances but not the already created ones. Try to place the set appearance code some place before your UINavigationConroller is created. it should still work.

chancyWu
  • 14,073
  • 11
  • 62
  • 81