UINavigationBar + UIToolBar UIAppearance, as well as translucent barStyle in some parts of the application?

I set 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 it view view view view. It should have a translucent navigation bar and toolbar, but instead it uses graphics that I provided with transparency.

The problem is that later, when I click on another view controller (after popping up to the super from PhotoViewer), its toolbar is also translucent, which means that the content is behind it.

I tried the following with no 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 on how I can get a black translucent barStyle for PhotoViewer only and save everything else with my own graphics?

UPDATE:. In an attempt to get some help, I collected a sample project with custom graphics for the navigation bar, and then tried to display a push-view controller with a transparent navigation bar without success when using the appearance proxy server: EXAMPLE OF THE PROJECT

+4
source share
2 answers

I uploaded your sample project and was able to fix it. I will explain what the problem is.

First of all, the UINavigationBar is contained within the UINavigationController. Thus, RootViewController and TranslucentViewController use the same instance of the UINavigationBar. Perhaps this is causing confusion. Also, probably why + the appearance of WhenContainedIn: doesn't work as you expect.

To set the background image of your navigation bars throughout your application, you must use + appearance. To set the background image of a single navigation bar in the navigation controller, use -setBackgroundImagE: forBarMetrics: from UINavigationBar.

Code: in -TranslucentViewController viewWillAppear: set the background image and panel style. In the RootViewController, set the background image and panel style again. In my experience, it's best to change the navigation bar in -viewWillAppear: instead of -in viewWillDisappear: (or you need to keep track of how to change it back.)

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 just change it in this place. Not by clicking on the view controller or something else.

+4
source

Depending on whether you use tabs, you can designate your appDelegate as a delegate to the navigation bar or a subclass of UINavigationController so that your code will know about all the changes in the viewController stack. Then you set the property to a UINavigationBar to make it transparent or opaque depending on the class of the visible viewController.

0
source

All Articles