0

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?

Sarah Wilson
  • 41
  • 2
  • 8
  • 1
    You say you have a navigation bar. But are you in fact inside a navigation controller? – matt Aug 11 '15 at 14:41
  • Yes, I am inside a navigation controller. – Sarah Wilson Aug 11 '15 at 14:45
  • Try it like this http://stackoverflow.com/questions/31846263/how-to-change-the-font-and-text-color-of-the-title-of-uinavigationbar – Laky Aug 11 '15 at 14:51
  • Ok, actually, now that you said that I went back and checked and it turned out I somehow had a navigation controller inside another navigation controller and that is what was causing the issue. I deleted it and my nav bar is working great now. I feel pretty dumb, I was stuck on that for a while. Thanks so much for your help! – Sarah Wilson Aug 11 '15 at 14:56

2 Answers2

2

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)

Jiri Trecak
  • 5,092
  • 26
  • 37
  • Yeah it was an issue with my NC. I somehow had a navigation controller inside another navigation controller and that is what was causing my issue. I deleted it and it's working great now. Thanks so much for your help! – Sarah Wilson Aug 11 '15 at 15:00
1

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

Jamil Hasnine Tamim
  • 4,389
  • 27
  • 43