How to change UINavigationBar background color fro

2019-01-08 11:35发布

I know how to change the UINavigationBar background image by doing

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nabbar"] forBarMetrics:UIBarMetricsDefault];

and I know how to set the bar to different colors within each Views..... Now I want to change the background color without using an image to a solid color from the app delegate. I do not want to set it each time from each view and I do not want to write a CGRect.

I tried [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]]; but I doesn't work and I cant find a code anywhere that works in the app delegate.

Could anyone please point me in the right direction?

9条回答
Animai°情兽
2楼-- · 2019-01-08 12:18

You can easily do this with Xcode 6.3.1. Select your NavigationBar in the Document outline. Select the Attributes Inspector. Uncheck Translucent. Set Bar Tint to your desired color. Done!

查看更多
闹够了就滚
3楼-- · 2019-01-08 12:19

For doing this in iOS 7:

[[UINavigationBar appearance] setBarTintColor:myColor];
查看更多
仙女界的扛把子
4楼-- · 2019-01-08 12:19

Swift:

self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
查看更多
时光不老,我们不散
5楼-- · 2019-01-08 12:25

As the other answers mention, you can use setTintColor:, but you want a solid color and that's not possible to do setting the tint color AFAIK.

The solution is to create an image programmatically and set that image as the background image for all navigation bars via UIAppearance. About the size of the image, I'm not sure if a 1x1 pixel image would work or if you need the exact size of the navigation bar.Check the second answer of this question to see how to create the image.

As an advice, I don't like to "overload" the app delegate with these type of things. What I tend to do is to create a class named AppearanceConfiguration with only one public method configureAppearance where I set all the UIAppearance stuff I want, and then I call that method from the app delegate.

查看更多
该账号已被封号
6楼-- · 2019-01-08 12:25

You can set UINavigation Background color by using this code in any view controller

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];
查看更多
地球回转人心会变
7楼-- · 2019-01-08 12:27

You can use [[UINavigationBar appearance] setTintColor:myColor];

Since iOS 7 you need to set [[UINavigationBar appearance] setBarTintColor:myColor]; and also [[UINavigationBar appearance] setTranslucent:NO].

[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];
查看更多
登录 后发表回答