Reputation: 1179
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?
Upvotes: 3
Views: 4053
Reputation: 554
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.
Upvotes: 0
Reputation: 12369
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.
Upvotes: 0
Reputation: 110
try this....
UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"NavBar_Img.png"]];
self.navigationItem.titleView = imageView;
[imageView release];
Upvotes: 5
Reputation: 20541
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];
Upvotes: 3