3

I've checked many solutions on line but I still have a problem with my NavigationController background image.

This is my code:

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

The problem is that my bar changed it's looks (color mainly) but it still doesn't look like my image.

Is there a better way to approach this?

Nimrod Shai
  • 1,149
  • 13
  • 26
  • chk my answer http://stackoverflow.com/questions/13488710/how-to-set-a-picture-programmatically-in-a-navbar/13488781#13488781 – Rajneesh071 Dec 04 '12 at 13:32
  • try adding the format of the image in the same code..that is [UIImage imageNamed:@"header_bg.png"]so that it takes the exact image. – kumareshm174 Dec 04 '12 at 13:37
  • Make sure the dimensions of the image match the the dimensions of the navigationBar, you can also use "[self.navigationController.navigationBar setClipsToBounds:YES];" if necessaryç You have forgotten ".png" in the image name by the way. – Yunus Nedim Mehel Dec 04 '12 at 13:51

4 Answers4

5

try this....

UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"NavBar_Img.png"]];
self.navigationItem.titleView = imageView;
[imageView release];
iOSDev1987
  • 110
  • 7
3

try to set style and at last add your code like bellow..

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg"] forBarMetrics:UIBarMetricsDefault];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Didn't work… still has some kind of gradient of the picture… Any idea how to remove it and make it more "flat"? (like my image) – Nimrod Shai Dec 04 '12 at 14:38
  • hey dude post the screenshot with image and also with your navigationbar which you get after this code.. – Paras Joshi Dec 05 '12 at 04:57
0

After playing with it some while I have realized that setting background image causes the navigation create a pattern of the image on it. If you add a smaller image on the bar the image will repeat itself or if you put a bigger one you will only see the top of it, this is why you can see the color but not the image.

Additionally as you change the background image, the background color of the search bar will be set as black and this causes some problems on semi-transparent images. Therefore try to avoid transparent images and try to use the same height for both the image and the bar, preferable 44 px.

Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
0

I had a slightly different problem which I share for fellow lost souls searching for the light o_O

My background image refused to show in the Navigation Bar after I showed a UIAlertView prior. I tried creating a separate View Controller and moved my UIAlertView into that but the same thing happened : no background image! The interesting thing was if I set the initial controller to the controller past the UIAlertView controller then the background image appeared as expected...

Thanks to Paras Joshi's answer below, instead of referring to

[UINavigationBar appearance]

I used...

self.navigationController.navigationBar 

and my background image appears once more regardless of the UIAlertView.

What are the difference between these two references? Was beginning to think there was an issue with iOS 7 that I needed to report.

Anthony De Souza
  • 544
  • 5
  • 10