Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5: Can you override UIAppearance customisations in specific classes?

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?

like image 347
lmirosevic Avatar asked Dec 10 '11 20:12

lmirosevic


3 Answers

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]];
like image 194
hypercrypt Avatar answered Oct 13 '22 19:10

hypercrypt


Move your changes to viewWillAppear: instead of viewDidLoad: and it should work.

like image 22
stevex Avatar answered Oct 13 '22 18:10

stevex


For that you would do:

id specialNavBarAppearance = [UINavigationBar appearanceWhenContainedIn:[SpecialViewController class], nil];

[specialNavBarAppearance setBarStyle:UIBarStyleBlack];
[specialNavBarAppearance setTranslucent:YES];
like image 24
Dave DeLong Avatar answered Oct 13 '22 18:10

Dave DeLong