iOS 11 上的导航栏使用大标题时更改状态栏背景颜色

Change statusBar background color while using large title for navigation bar on iOS 11

我正尝试在 iOS 11.

上使用新导航栏的大标题功能

但是,在我添加以下行之后:

self.navigationController?.navigationBar.prefersLargeTitles = true

我发现navigationBar的背景色变成了黑色

所以我再次手动设置背景颜色:

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

但是,我发现statusBar的背景颜色并没有改变:

我通过这段代码设置statusBar的背景色后:

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

它在状态栏和导航栏之间给我一条难看的 1px 黑线:

那么navigationBar背景色的正确设置方法是什么?

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

self.navigationController?.navigationBar.barTintColor = .red

您可能会注意到您设置的颜色可能会有些褪色。如文档中所述:

This color is made translucent by default unless you set the isTranslucent property to false.

See the barTintColor reference on developer.apple.com