简体   繁体   English

如何从 AppDelegate 更改 UINavigationBar 背景颜色

[英]How to change UINavigationBar background color from the AppDelegate

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 .而且我知道如何在每个Views中将栏设置为不同的颜色......现在我想在不使用图像的情况下将背景颜色更改为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]];我试过[[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?有人可以指出我正确的方向吗?

You can use [[UINavigationBar appearance] setTintColor:myColor]; 你可以使用[[UINavigationBar appearance] setTintColor:myColor];

Since iOS 7 you need to set [[UINavigationBar appearance] setBarTintColor:myColor]; 从iOS 7开始,你需要设置[[UINavigationBar appearance] setBarTintColor:myColor]; and also [[UINavigationBar appearance] setTranslucent:NO] . 还有[[UINavigationBar appearance] setTranslucent:NO]

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

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];

要在iOS 7中执行此操作:

[[UINavigationBar appearance] setBarTintColor:myColor];

Swift syntax: Swift语法:

    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中,它在整个应用程序中都存在

You can easily do this with Xcode 6.3.1. 您可以使用Xcode 6.3.1轻松完成此操作。 Select your NavigationBar in the Document outline. 在文档大纲中选择您的NavigationBar。 Select the Attributes Inspector. 选择“属性”检查器。 Uncheck Translucent. 取消选中半透明。 Set Bar Tint to your desired color. 将Bar Tint设置为所需的颜色。 Done! 完成!

Swift : 斯威夫特

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

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 . 解决方案是以编程方式创建图像,并通过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. 关于图像的大小,我不确定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,然后我从app delegate调用该方法。

iOS 13.0 introduced new API for this: iOS 13.0为此引入了新的 API:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let myColor = UIColor(hue: 0.4, saturation: 0.25, brightness: 1, alpha: 1)
    
    let barAppearance = UINavigationBarAppearance()
    barAppearance.backgroundColor = myColor

    let navigationBar = UINavigationBar.appearance()
    navigationBar.standardAppearance = barAppearance
    navigationBar.scrollEdgeAppearance = barAppearance // for scrollable content or large titles
    
    return true
}

导航栏

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

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

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 这里

tintColor is for to set background images like back button & menu lines images etc. (See below left and right menu image) tintColor用于设置背景图像,如后退按钮和菜单行图像等。(见下左右菜单图像)

barTintColor is 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

在此输入图像描述

Dealing with UINavigationController and UINavigationBar in iOS is a hassle.在 iOS 中处理 UINavigationController 和 UINavigationBar 很麻烦。 Fortunately, having an open source third-party library can easily solve these problems, hoping to help everyone.好在有开源的第三方库可以轻松解决这些问题,希望对大家有所帮助。

Git repo: NXNavigationExtension Git 存储库: NXNavigationExtension

Change UINavigationBar color:更改 UINavigationBar 颜色:

extension YourViewController {
    
    override var nx_titleTextAttributes: [NSAttributedString.Key : Any]? {
        return [NSAttributedString.Key.foregroundColor: .red]
    }
    
}

📝 example 📝 例子

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. 而不是使用195/255,使用0.7647或195.f / 255.f问题是转换浮动不能正常工作。 Try using exact float value. 尝试使用精确浮点值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM