Changing the Color of Navigation Bar in Swift

8.7k views Asked by At

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. 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?

2

There are 2 answers

1
Jiri Trecak On BEST ANSWER

It is very probably because your self.navigationController is empty. 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. You can go around that problem by using .appearance() interface, like this:

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)

0
Jamil Hasnine Tamim On

Updated for 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

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

Also can check here : https://github.com/hasnine/iOSUtilitiesSource