Reputation: 14886
I am setting custom images for my UINavigationBar
and UIToolbar
using the following code:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"toolbar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
I am using PhotoViewer and pushing its view controller into view. It should have a translucent navigationbar and toolbar, but instead it uses the graphics I have supplied with translucency.
The problem is that later when I push in another view controller (after popping back to super from the PhotoViewer) its toolbar is also translucent which means the content sits behind it.
I have tried the following without luck:
[[UINavigationBar appearanceWhenContainedIn:[EGOPhotoViewController class], nil] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearanceWhenContainedIn:[EGOPhotoViewController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearanceWhenContainedIn:[EGOPhotoViewController class], nil] setBarStyle:UIBarStyleBlackTranslucent];
[[UIToolbar appearanceWhenContainedIn:[EGOPhotoViewController class], nil] setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Any ideas how I can achieve a black translucent barStyle just for the PhotoViewer and keep everything else with my custom graphics?
UPDATE: In an attempt to get some help, I've put together a sample project with a custom graphic for the navigation bar and then trying to display a pushed view controller with a translucent navigation bar without success when using the appearance proxy: EXAMPLE PROJECT
Upvotes: 4
Views: 4158
Reputation: 361
I've downloaded your sample project and managed to fix it. I'll explain what the problem is.
First of all, the UINavigationBar is contained inside the UINavigationController. So, the RootViewController and the TranslucentViewController are using the same UINavigationBar instance. Perhaps this causes confusion. Also, that's probably why the +appearanceWhenContainedIn: is not working as you expect.
To set the background image of your navigation bars throughout your whole application, you should use +appearance. To set the background image of a single navigation bar in a navigation controller, use -setBackgroundImagE:forBarMetrics: of the UINavigationBar.
The code: in -TranslucentViewController viewWillAppear: set the background image and the bar style. In RootViewController, set the background image and the bar style again. In my experience it's the best to change the navigation bar in -viewWillAppear: instead of -in viewWillDisappear: (or you need to keep track of what to change it back to.)
In RootViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"orangeNavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}
In TranslucentViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
}
Oh and: only change it in these place. Not when pushing the view controller or anything.
Upvotes: 4
Reputation: 41642
Depending on whether or not you use a tabbar too, you can designate your appDelegate as the delegate of the navigation bar, or subclass UINavigationController, so your code becomes aware of all changes to the viewController stack. Then, you set the property on the UINavigationBar to make it transparent or non-transparent depending on the class of the visible viewController.
Upvotes: 0