简体   繁体   English

在 Xcode 中更改导航栏颜色

[英]Changing Navigation Bar Color in Xcode

I am building a Swift app and I am trying to change the color of the navigation bar.我正在构建一个 Swift 应用程序,我正在尝试更改导航栏的颜色。 I'm trying to do it directly from Xcode without code on the main.storyboard.我正在尝试直接从 Xcode 执行此操作,而 main.storyboard 上没有代码。

I've tried changing several settings in the Xcode Attribute Inspector but nothing has worked.我已经尝试更改 Xcode 属性检查器中的几个设置,但没有任何效果。 I've also searched StackOverFlow but all the answers I find are are changing it programatically.我还搜索了 StackOverFlow,但我找到的所有答案都是以编程方式更改它。

Does anyone know how to change the color of it directly from the Attribute Inspector?有谁知道如何直接从属性检查器更改它的颜色?

Thanks.谢谢。

Try it:试试看:

   if #available(iOS 13.0, *) {
                let appearance = UINavigationBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = UIColor(red: 21/255, green: 46/255, blue: 82/255, alpha: 1)
                appearance.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont(name: "Arial", size: 32)!]
                navigationController?.navigationBar.standardAppearance = appearance
                navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
                navigationController?.navigationBar.compactAppearance = appearance.copy()
}

You can use您可以使用

navigationBar.barTintColor = UIColor.whiteColor()

or you do this by Storyboard Like this.或者你通过 Storyboard 来做这个,就像这样。

在此处输入图像描述

In the storyboard change the background color like this.在 storyboard 中像这样更改背景颜色。 It will work.它会起作用的。 enter image description here在此处输入图像描述

You just need to click on the navbar and in attribute inspector, there is an option for Background, you can provide any color which you want to give.你只需要点击导航栏和属性检查器,有一个背景选项,你可以提供你想要的任何颜色。 In my case, I selected a custom sky blue color.就我而言,我选择了自定义的天蓝色。 在此处检查屏幕截图

Navigation Bar:导航栏:

navigationController?.navigationBar.barTintColor = UIColor.green

Replace greenColor with whatever UIColor you want用你想要的任何 UIColor 替换 greenColor

Navigation Bar Text:导航栏文字:

navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]

Replace orangeColor with whatever color you like.将 orangeColor 替换为您喜欢的任何颜色。

Tab Bar:标签栏:

tabBarController?.tabBar.barTintColor = UIColor.brown

Tab Bar Text:标签栏文本:

tabBarController?.tabBar.tintColor = UIColor.yellow

On the last two, replace brownColor and yellowColor with the color of your choice.在最后两个中,将 brownColor 和 yellowColor 替换为您选择的颜色。

swift navigation bar title color: swift 导航栏标题颜色:

Place this in your didFinishLaunchingWithOptions method in the AppDelegate.将它放在 AppDelegate 中的 didFinishLaunchingWithOptions 方法中。

let attrs = [
  NSAttributedString.Key.foregroundColor: UIColor.white
]

UINavigationBar.appearance().titleTextAttributes = attrs

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

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