问题
I'm building an app with many view controllers: I have a UITabBarController which holds 4 UINavigationController. I want all the nav bars to be my custom color, say blue, which I achieve by doing this in my app delegate:
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
But I also have one special view controller which has a map, and for this view controller I want to override the blue navbar set using UIAppearance to make it the black opaque style. I've tried by calling this inside viewDidLoad:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationController.navigationBar.translucent = YES;
But nothing happens. Can this be done or do I have to abandon UIAppearance and set the properties on navigationBar manually for each view controller?
回答1:
The way you are doing it is supposed to work, but it doesn't. This does work though:
Swift 4
UINavigationBar.appearance(whenContainedInInstancesOf: [YourOtherVC.self]).tintColor = .black
Objective-C
[[UINavigationBar appearanceWhenContainedIn:[YourOtherVC class], nil] setTintColor:[UIColor blackColor]];
回答2:
Move your changes to viewWillAppear: instead of viewDidLoad: and it should work.
回答3:
For that you would do:
id specialNavBarAppearance = [UINavigationBar appearanceWhenContainedIn:[SpecialViewController class], nil];
[specialNavBarAppearance setBarStyle:UIBarStyleBlack];
[specialNavBarAppearance setTranslucent:YES];
来源:https://stackoverflow.com/questions/8459821/ios-5-can-you-override-uiappearance-customisations-in-specific-classes