NavigationBar bar, tint, and title text color in iOS 8

247,347

Solution 1

In AppDelegate.swift, in application(_:didFinishLaunchingWithOptions:) I put the following:

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

(For Swift 4 or earlier use NSAttributedStringKey instead of NSAttributedString.Key)

For titleTextAttributes, the docs say:

You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary

Solution 2

I like Alex's answer. If you want something quick to try out in a ViewController make sure you use

viewWillAppear()
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var nav = self.navigationController?.navigationBar
    nav?.barStyle = UIBarStyle.Black
    nav?.tintColor = UIColor.white
    nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
    //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
}

enter image description here

Solution 3

To change the color universally, this code should sit in the NavigationController's viewDidLoad function:

class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Status bar white font
        self.navigationBar.barStyle = UIBarStyle.Black
        self.navigationBar.tintColor = UIColor.whiteColor()
    }
}

To change it per ViewController you would have to reference the NavigationController from the ViewController and write similar lines in that ViewController's viewWillAppear function.

Solution 4

Swift 5

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

Swift 4

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

Solution 5

To work in objective-c I have to put the following lines in viewWillAppear in my CustomViewController.

[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setTranslucent:NO];

For Swift2.x this works:

self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

For Swift3.x this works:

self.navigationController?.navigationBar.barTintColor = UIColor.red
Share:
247,347

Related videos on Youtube

AG1
Author by

AG1

Coding.

Updated on July 08, 2022

Comments

  • AG1
    AG1 almost 2 years

    The background text in the status bar is still black. How do I change the color to white?

    // io8, swift, Xcode 6.0.1 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]
    
    }
    

    enter image description here

  • AG1
    AG1 over 9 years
    How do I override the NavigationController to use these methods?
  • Alex
    Alex over 9 years
    You don't override the NavigationController, you just modify it in the ViewDidLoad function. I updated my answer above.
  • Saorikido
    Saorikido over 9 years
    But I'm using iso 8.1
  • AG1
    AG1 over 9 years
    So a developer just has to add NavigationController.swift to the workspace? If so that is neat!
  • kakubei
    kakubei over 9 years
    @AG1 This is incorrect, just adding a new file NavigationController.swift is not enough, you have to hook up your Navigation Controller in the Storyboard to this file in the Identity Inspector since by default it will use a Generic UINavigationController.
  • Adam Johns
    Adam Johns almost 9 years
    why viewWillAppear()? I am doing this in viewDidLoad and it works fine.
  • user3163404
    user3163404 over 8 years
    But when I go back to another viewcontroller the color has changed also. Any idea on how to prevent this. I want mine main viewcontroller to be one color and the others another.
  • Niko Klausnitzer
    Niko Klausnitzer over 8 years
    I think you also have to set the color in your new view controller.
  • FractalDoctor
    FractalDoctor over 8 years
    Because every time you view the UIView you'd like it to be set. If you set in viewDidLoad() say in a UITabBar view, move to another tab where it's set again, then come back it'll be overwritten as the original view was only loaded once.
  • sumit
    sumit about 8 years
    what if we have mutiple navigation bars ?
  • triiiiista
    triiiiista almost 8 years
    tintColor doesn't work for Xcode 7.3.1. The text is still black.
  • Markymark
    Markymark over 7 years
    You can also just do all of this in the storyboard w/out needing to create a custom navigation controller. The properties for Bar Tine and Style are right there on the right under the Attributes inspector.
  • oky_sabeni
    oky_sabeni over 7 years
    In Xcode 8.2, the titleTextAttributes does not autocomplete but it works.
  • Chauyan
    Chauyan over 7 years
    Yes, either you put your setting value in UINavigationController ViewDidLoad or you put these setting value in UIViewController ViewWillApear function, both work for me
  • Ahmadreza
    Ahmadreza over 6 years
    I get: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor'
  • Alan Scarpa
    Alan Scarpa about 6 years
    Update for swift 4 - NSForegroundColorAttributeName should now be NSAttributedStringKey.foregroundColor
  • Sylvan D Ash
    Sylvan D Ash about 6 years
    NOTE: this will change the title color for all controllers, even the parent. If you want the parent to still retain it's color, you can do so in the willMove(toParentViewController parent: UIViewController?) function of the child
  • 31piy
    31piy almost 6 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • rgkobashi
    rgkobashi over 5 years
    is barTintColor still necessary? deleting that line still works
  • Bhavsang Jam
    Bhavsang Jam over 5 years
    Please use below code for latest version xcode 10 and swift 4.2 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  • Stephane Paquet
    Stephane Paquet over 5 years
    Update for Swift 4.2: NSAttributedString.Key.foregroundColor instead of NSForegroundColorAttributeName
  • Ben
    Ben about 5 years
    I really liked how you added this answer. .barTintColor isn't an option now. Do you mean .backgroundColor?
  • Ram Madhavan
    Ram Madhavan over 3 years
    This is adding a text attribute. Not actually changing the color of the title. If you note the font of the text you can understand the difference.
  • Ram Madhavan
    Ram Madhavan over 3 years
    This is adding a text attribute. Not actually changing the color of the title. If you note the font of the text you can understand the difference.
  • Muhammad Danish Qureshi
    Muhammad Danish Qureshi over 2 years
    @Albert Vila Calvo in iOS 15 this thing is not working, can you tell me how to do that?