ios 如何从 AppDelegate 更改 UINavigationBar 背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17014713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change UINavigationBar background color from the AppDelegate
提问by Jonathan Thurft
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 imageto 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
.
并且我知道如何将条形图设置为每个条形中的不同颜色Views
..... 现在我想在不使用图像的情况下将背景颜色更改为app delegate
. 我不想每次都从每个视图设置它,我不想写一个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]];
但我不工作,我无法在应用程序委托中的任何地方找到代码。
Could anyone please point me in the right direction?
有人可以指出我正确的方向吗?
回答by Seb Thiebaud
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 appearance] setTranslucent:NO]
。
[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];
回答by LJ1
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];
回答by Lasse Bunk
For doing this in iOS 7:
要在 iOS 7 中执行此操作:
[[UINavigationBar appearance] setBarTintColor:myColor];
回答by Dustin Williams
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 中,它在整个应用程序中持续存在
回答by Hemang
Swift:
斯威夫特:
self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
回答by ronm333
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。选择属性检查器。取消选中半透明。将条形色调设置为您想要的颜色。完毕!
回答by e1985
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:
,但您想要纯色,并且无法设置色调颜色 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 questionto 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.
作为一个建议,我不喜欢用这些类型的东西“重载”应用程序委托。我倾向于做的是创建一个AppearanceConfiguration
仅使用一个公共方法命名的类,configureAppearance
在其中设置我想要的所有 UIAppearance 内容,然后从应用程序委托调用该方法。
回答by iOS
In Swift 4.2 and Xcode 10.1
在 Swift 4.2 和 Xcode 10.1 中
You can change your navigation bar colour from your AppDelegate directly to your entire project.
您可以将导航栏颜色从 AppDelegate 直接更改为整个项目。
In didFinishLaunchingWithOptions launchOptions:
write below to lines of code
在didFinishLaunchingWithOptions launchOptions:
下面写到代码行
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
Here
这里
tintColoris for to set background images like back button & menu lines images etc. (See below left and right menu image)
tintColor用于设置背景图像,如后退按钮和菜单行图像等(见下方左右菜单图像)
barTintColoris for navigation bar background colour
barTintColor用于导航栏背景颜色
If you want to set specific view controller navigation bar colour, write below code in viewDidLoad()
如果要设置特定的视图控制器导航栏颜色,请在以下代码中写入 viewDidLoad()
//Add navigation bar colour
navigationController?.navigationBar.barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
navigationController?.navigationBar.tintColor = UIColor.white
回答by amar
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];
回答by ganka
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.
颜色代码是这里的问题。不要使用 195/255,而是使用 0.7647 或 195.f/255.f 问题是转换浮点数无法正常工作。尝试使用精确的浮点值。