2017-09-16 35 views
7

我試圖使用新的導航欄的標題大功能在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

那麼,什麼是設置的導航欄的背景顏色的正確方法是什麼?

回答

4

設置UINavigationBar的背景顏色的正確方法是使用barTintColor屬性。

self.navigationController?.navigationBar.barTintColor = .red 

您可能會注意到,您設置的顏色可能有點褪色。正如文件中指出:除非你isTranslucent屬性設置爲false

這種顏色是由默認半透明。

See the barTintColor reference on developer.apple.com

相關問題