Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar with solid color - ios 7

I'm using an UINavigationController with its default UINavigationBar and I'm trying to turn the translucent effect off, so I can have a solid color on it. Apparently, it doesn't work with code but it is possible with storyboard with just one click (weird ?!!)

my code:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:APP_DELEGATE.map];
[navController setToolbarHidden:YES];
[navController.navigationBar setTranslucent:NO];
[navController.navigationBar setBarTintColor:[UIColor turquoiseColor]];
//    [[UINavigationBar appearance] setBarTintColor:[UIColor turquoiseColor]]; | also doesn't work

Why doesn't it work ? How can I fix that ? Thanks.

like image 958
Pham Hoan Avatar asked May 30 '14 03:05

Pham Hoan


3 Answers

This is the right way to do it. User appearance proxy methods.

[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[self.navigationController.navigationBar setTranslucent:NO];

Thats all & you are done. Use whatever colour you want. The above code makes the NavigationBar as opaque (i.e. Solid color).

like image 171
Balram Tiwari Avatar answered Sep 28 '22 18:09

Balram Tiwari


And for all you Swiftians out there:

UINavigationBar.appearance().barStyle = UIBarStyle.Default
UINavigationBar.appearance().barTintColor = UIColor.redColor()
self.navigationController?.navigationBar.translucent = false

The above should work for Swift 1.2 and Swift 2.

like image 41
jengelsma Avatar answered Sep 28 '22 16:09

jengelsma


use this way for solid color of navigation bar i have faced this issue lots of time

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:APP_DELEGATE.map];
[navController setToolbarHidden:YES]; 
navController.navigationBar.barTintColor =[UIColor turquoiseColor];
[navController.navigationBar setTranslucent:NO];
like image 32
Kalpit Gajera Avatar answered Sep 28 '22 16:09

Kalpit Gajera