如何从AppDelegate中更改UINavigationBar背景颜色

时间:2022-11-20 23:06:02

I know how to change the UINavigationBar background image by doing

我知道如何通过做来改变UINavigationBar的背景图像

[[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.

我知道如何在每个视图中设置不同颜色的栏…现在我想改变背景颜色,不使用图像从应用委托变成纯色。我不希望每次从每个视图设置它,也不希望编写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.

我尝试[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]];但我不工作,也找不到在app委托中工作的代码。

Could anyone please point me in the right direction?

谁能告诉我正确的方向吗?

9 个解决方案

#1


185  

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

可以使用[[UINavigationBar appearance] setTintColor:myColor];

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

由于iOS 7,你需要设置[[[UINavigationBar appearance] setBarTintColor:myColor];还有[UINavigationBar外观]settransparent:NO]。

[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];

#2


97  

To change the background color and not the tint the following piece of code will work:

要改变背景颜色,而不是修改下面的代码,下面的代码将会起作用:

[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTranslucent:NO];

#3


17  

For doing this in iOS 7:

在iOS 7中这么做:

[[UINavigationBar appearance] setBarTintColor:myColor];

#4


12  

Swift syntax:

斯威夫特的语法:

    UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color

I just put that in the AppDelegate didFinishLaunchingWithOptions and it persists throughout the app

我只是把它放到AppDelegate didFinishLaunchingWithOptions中它在整个应用中都存在

#5


6  

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!

使用Xcode 6.3.1可以很容易地做到这一点。在文档大纲中选择NavigationBar。选择属性检查器。取消半透明的。将酒吧色调设置为你想要的颜色。完成了!

#6


3  

Swift:

迅速:

self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false

#7


2  

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.

正如其他答案所提到的,您可以使用setTintColor:,但是您想要一种纯色,而这是不可能设置颜色的。

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.

解决方案是以编程方式创建一个映像,并通过UIAppearance将该映像设置为所有导航条的背景映像。关于图像的大小,我不确定1x1像素的图像是否有效,或者是否需要导航条的精确尺寸。检查这个问题的第二个答案,看看如何创建映像。

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.

作为一个建议,我不喜欢用这些东西“超载”应用委托。我要做的是创建一个类,名为appearance econfiguration,它只有一个公共方法configureAppearance,我在这里设置了我想要的所有UIAppearance内容,然后我从app委托调用这个方法。

#8


2  

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

可以在任何视图控制器中使用此代码设置UINavigation背景色

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];

#9


0  

The colour code is the issue here. Instead of using 195/255, use 0.7647 or 195.f/255.f The problem is converting the float is not working properly. Try using exact float value.

颜色编码是这里的问题。使用0.7647或195.f/255代替195/255。f问题是转换浮点数不能正常工作。尝试使用精确的浮点值。

#1


185  

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

可以使用[[UINavigationBar appearance] setTintColor:myColor];

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

由于iOS 7,你需要设置[[[UINavigationBar appearance] setBarTintColor:myColor];还有[UINavigationBar外观]settransparent:NO]。

[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];

#2


97  

To change the background color and not the tint the following piece of code will work:

要改变背景颜色,而不是修改下面的代码,下面的代码将会起作用:

[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTranslucent:NO];

#3


17  

For doing this in iOS 7:

在iOS 7中这么做:

[[UINavigationBar appearance] setBarTintColor:myColor];

#4


12  

Swift syntax:

斯威夫特的语法:

    UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color

I just put that in the AppDelegate didFinishLaunchingWithOptions and it persists throughout the app

我只是把它放到AppDelegate didFinishLaunchingWithOptions中它在整个应用中都存在

#5


6  

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!

使用Xcode 6.3.1可以很容易地做到这一点。在文档大纲中选择NavigationBar。选择属性检查器。取消半透明的。将酒吧色调设置为你想要的颜色。完成了!

#6


3  

Swift:

迅速:

self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false

#7


2  

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.

正如其他答案所提到的,您可以使用setTintColor:,但是您想要一种纯色,而这是不可能设置颜色的。

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.

解决方案是以编程方式创建一个映像,并通过UIAppearance将该映像设置为所有导航条的背景映像。关于图像的大小,我不确定1x1像素的图像是否有效,或者是否需要导航条的精确尺寸。检查这个问题的第二个答案,看看如何创建映像。

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.

作为一个建议,我不喜欢用这些东西“超载”应用委托。我要做的是创建一个类,名为appearance econfiguration,它只有一个公共方法configureAppearance,我在这里设置了我想要的所有UIAppearance内容,然后我从app委托调用这个方法。

#8


2  

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

可以在任何视图控制器中使用此代码设置UINavigation背景色

self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];

#9


0  

The colour code is the issue here. Instead of using 195/255, use 0.7647 or 195.f/255.f The problem is converting the float is not working properly. Try using exact float value.

颜色编码是这里的问题。使用0.7647或195.f/255代替195/255。f问题是转换浮点数不能正常工作。尝试使用精确的浮点值。