简体   繁体   English

在 Swift 中更改导航栏的颜色

[英]Changing the Color of Navigation Bar in Swift

I have a MKMap in my ViewController and I allow my users to switch map types between "Standard", "Hybrid", and "Satellite" - much like in the Maps app.我的 ViewController 中有一个 MKMap,我允许我的用户在“标准”、“混合”和“卫星”之间切换地图类型——就像在地图应用程序中一样。 Like in the maps app I have a bottom bar, and then I have a navigation bar at the top.就像在地图应用程序中一样,我有一个底部栏,然后顶部有一个导航栏。 When the user switches between map types, the bars should change to a black background with white buttons for "Hybrid" and "Satellite", and a white background with blue buttons for "Standard.当用户在地图类型之间切换时,条形图应更改为带有“混合”和“卫星”按钮的黑色背景,以及带有“标准”按钮的白色背景和蓝色按钮。

I have the bottom bar changing colors correctly but am unable to get the navigation bar to change at all.我有底部栏正确更改颜色,但根本无法更改导航栏。 Here is my function for when a user changes map types:这是我在用户更改地图类型时的功能:

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

} }

Does anyone know what I must do differently to get my nav bar to change colors?有谁知道我必须做些什么不同的事情才能让我的导航栏改变颜色?

It is very probably because your self.navigationController is empty.这很可能是因为您的 self.navigationController 是空的。 Try that first!先试试吧! It is o-so-very common problem.这是非常普遍的问题。 Maybe you are calling it from wrong view controller that in fact does not have NC.也许您是从错误的视图控制器调用它,而实际上它没有 NC。 You can go around that problem by using .appearance() interface, like this:您可以使用 .appearance() 接口来解决该问题,如下所示:

To change bar tint color (background of navigation bar):要更改栏色调颜色(导航栏的背景):

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

To change color of the texts:要更改文本的颜色:

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

Be aware that this changes navigation bar for entire application (basically, appearance is the way how to change default setting), so it might not be appropriate.请注意,这会更改整个应用程序的导航栏(基本上,外观是更改默认设置的方式),因此它可能不合适。 If you are interested, here you can read more about appearance .如果您有兴趣,可以在此处阅读有关外观的更多信息

Hope it helps!希望能帮助到你!

Edit: Easy way how to check for empty navigation controller would be force-unwrapping the variable, self.navigationController!.navigationBar instead of ?, if your app crashes, your problem lies here (but don't tell to anyone I suggested that as it is not very slick solution how to find the problem, though fast)编辑:如何检查空导航控制器的简单方法是强制解开变量 self.navigationController!.navigationBar 而不是 ?,如果您的应用程序崩溃,您的问题就在这里(但不要告诉任何人我建议作为如何找到问题不是很巧妙的解决方案,虽然很快)

Updated for Swift 3, 4, 4.2, 5+为 Swift 3、4、4.2、5+ 更新

// setup navBar..... // 设置导航栏.....

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

Swift 4斯威夫特 4

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

Swift 4.2, 5+斯威夫特 4.2, 5+

UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false

Also can check here : https://github.com/hasnine/iOSUtilitiesSource也可以在这里查看: https : //github.com/hasnine/iOSUtilitiesSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM