更改状态栏。背景颜色,而使用大标题在iOS 11导航栏

问题描述:

我试图使用新的导航栏的标题大功能在iOS上11更改状态栏。背景颜色,而使用大标题在iOS 11导航栏

然而,当我加入以下行:

self.navigationController?.navigationBar.prefersLargeTitles = true 

我发现navigationBar背景颜色变成了黑色。

Navigation Bar 1

因此我开始再次手动背景色:

self.navigationController?.setBackgroundColor(UIColor(hexString: 0xFF7E79)) 

然而,我发现,状态栏的背景颜色没有改变:

Navigation Bar 2

后,我设置通过以下代码添加statusBar的背景颜色:

guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return 
statusBar.backgroundColor = UIColor(hexString: 0xFF7E79) 

它带给我的状态栏和的的导航栏之间的丑陋1px的​​黑线是这样的:

Navigation Bar 3

那么,什么是设置的导航栏的背景颜色的正确方法是什么?

设置UINavigationBar的背景颜色的正确方法是使用barTintColor属性。

self.navigationController?.navigationBar.barTintColor = .red 

您可能会注意到,您设置的颜色可能有点褪色。正如文件中指出:除非你isTranslucent属性设置为false

这种颜色是由默认半透明。

See the barTintColor reference on developer.apple.com