首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Swift中更改导航栏颜色

在Swift中更改导航栏颜色
EN

Stack Overflow用户
提问于 2014-07-11 06:18:45
回答 15查看 346.8K关注 0票数 288

我使用一个拾取器视图,以允许用户选择整个应用程序的颜色主题。

我计划改变导航栏,背景和标签栏的颜色(如果可能的话)。

我一直在研究如何做到这一点,但找不到任何Swift示例。谁能给我一个例子,我需要用来改变导航栏颜色和导航栏文本颜色的代码?

Picker视图已设置,我只是寻找代码来改变用户界面的颜色。

EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2014-07-11 06:48:41

导航栏:

代码语言:javascript
复制
navigationController?.navigationBar.barTintColor = UIColor.green

将greenColor替换为您想要的任何UIColor,如果您愿意,也可以使用RGB。

导航栏文本:

代码语言:javascript
复制
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]

用你喜欢的任何颜色替换orangeColor。

选项卡栏:

代码语言:javascript
复制
tabBarController?.tabBar.barTintColor = UIColor.brown

选项卡栏文本:

代码语言:javascript
复制
tabBarController?.tabBar.tintColor = UIColor.yellow

在最后两个颜色中,将brownColor和yellowColor替换为您选择的颜色。

票数 614
EN

Stack Overflow用户

发布于 2014-07-11 06:56:59

下面是一些非常基本的外观定制,您可以在整个应用程序中应用它们:

代码语言:javascript
复制
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();

Swift 5.4.2:

代码语言:javascript
复制
UINavigationBar.appearance().backgroundColor = .green // backgorund color with gradient
// or
UINavigationBar.appearance().barTintColor = .green  // solid color
    
UIBarButtonItem.appearance().tintColor = .magenta
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
UITabBar.appearance().barTintColor = .yellow

有关Swift中UIAppearance应用编程接口的更多信息,请阅读here

票数 94
EN

Stack Overflow用户

发布于 2015-08-10 15:24:19

代码语言:javascript
复制
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

只需将此行粘贴到代码的didFinishLaunchingWithOptions中即可。

票数 52
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24687238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档