[英]Changing the Color of Navigation Bar in Swift
我的 ViewController 中有一個 MKMap,我允許我的用戶在“標准”、“混合”和“衛星”之間切換地圖類型——就像在地圖應用程序中一樣。 就像在地圖應用程序中一樣,我有一個底部欄,然后頂部有一個導航欄。 當用戶在地圖類型之間切換時,條形圖應更改為帶有“混合”和“衛星”按鈕的黑色背景,以及帶有“標准”按鈕的白色背景和藍色按鈕。
我有底部欄正確更改顏色,但根本無法更改導航欄。 這是我在用戶更改地圖類型時的功能:
func changeMapType(sender:UISegmentedControl){
if mapType.selectedSegmentIndex == 0{
mapView.mapType = MKMapType.Standard
toolbar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
toolbar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
defaults.setValue("Standard", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 1{
mapView.mapType = MKMapType.Hybrid
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Hybrid", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 2{
mapView.mapType = MKMapType.Satellite
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Satellite", forKey: "initialMapType")
}
}
有誰知道我必須做些什么不同的事情才能讓我的導航欄改變顏色?
這很可能是因為您的 self.navigationController 是空的。 先試試吧! 這是非常普遍的問題。 也許您是從錯誤的視圖控制器調用它,而實際上它沒有 NC。 您可以使用 .appearance() 接口來解決該問題,如下所示:
要更改欄色調顏色(導航欄的背景):
UINavigationBar.appearance().barTintColor = UIColor.whiteColor()
要更改文本的顏色:
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
請注意,這會更改整個應用程序的導航欄(基本上,外觀是更改默認設置的方式),因此它可能不合適。 如果您有興趣,可以在此處閱讀有關外觀的更多信息。
希望能幫助到你!
編輯:如何檢查空導航控制器的簡單方法是強制解開變量 self.navigationController!.navigationBar 而不是 ?,如果您的應用程序崩潰,您的問題就在這里(但不要告訴任何人我建議作為如何找到問題不是很巧妙的解決方案,雖然很快)
為 Swift 3、4、4.2、5+ 更新
// 設置導航欄.....
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
斯威夫特 4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
斯威夫特 4.2, 5+
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
也可以在這里查看: https : //github.com/hasnine/iOSUtilitiesSource
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.