在Swift中改变导航栏的颜色

时间:2015-08-11 14:38:50

标签: ios swift uinavigationbar tintcolor bartintcolor

我在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")
}

}

有没有人知道我必须做些什么才能让我的导航栏改变颜色?

2 个答案:

答案 0 :(得分:1)

很可能是因为你的self.navigationController是空的。先试试吧!这是非常常见的问题。也许你是从错误的视图控制器调用它实际上没有NC。您可以使用.appearance()接口来解决这个问题,如下所示:

更改条纹色调(导航栏背景):

UINavigationBar.appearance().barTintColor = UIColor.whiteColor()

改变文本的颜色:

UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]

请注意,这会更改整个应用程序的导航栏(基本上,外观是如何更改默认设置的方式),因此可能不合适。如果您有兴趣,可以read more about appearance

希望它有所帮助!

编辑:如何检查空导航控制器的简单方法是强制解包变量self.navigationController!.navigationBar而不是?,如果你的应用程序崩溃了,你的问题就在这里(但不要告诉他们我建议的任何人,因为它不是很光滑的解决方案如何找到问题,虽然快速)

答案 1 :(得分:1)

已更新为Swift 3、4、4.2、5 +

//设置navBar .....

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

Swift 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

相关问题