首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:无法更改导航栏的颜色

iOS:无法更改导航栏的颜色
EN

Stack Overflow用户
提问于 2022-01-24 13:11:13
回答 1查看 2.7K关注 0票数 -1

无法更改导航栏的背景色。在下面的示例中,我尝试将颜色设置为navigationController?.navigationBar.backgroundColor和navigationController?.navigationBar.barTintColor,,例如UIColor.red,但是在视图层次结构中的导航栏颜色上方出现了白色的UIImage,如下图所示:https://imguh.com/image/CBXaj

也尝试使导航条半透明和不透明,没有效果。我可以更改按钮颜色和自定义其他元素,但不能更改条形条的背景色。感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2022-01-24 15:09:21

下面的代码应该对您有好处:

背景色

代码语言:javascript
复制
// This will change the navigation bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.green // your colour here

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance

将其放置在viewDidLoad()函数中的ViewController中

标题颜色

如果还想更改导航栏标题外观,请在设置standardAppearancescrollEdgeAppearance之前应用以下代码

代码语言:javascript
复制
// This will alter the navigation bar title appearance
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.purple] //alter to fit your needs
appearance.titleTextAttributes = titleAttribute

完整代码:

代码语言:javascript
复制
// This will change the navigation bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.green
        
// This will alter the navigation bar title appearance
let titleAttribute = [NSAttributedString.Key.font:  UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.purple] //alter to fit your needs
appearance.titleTextAttributes = titleAttribute

navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance

图像示例

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

https://stackoverflow.com/questions/70834421

复制
相关文章

相似问题

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