192

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

Naresh
  • 16,698
  • 6
  • 112
  • 113
AG1
  • 6,648
  • 8
  • 40
  • 57

18 Answers18

331

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

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
130

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

AG1
  • 6,648
  • 8
  • 40
  • 57
  • But I'm using iso 8.1 – Saorikido Nov 05 '14 at 15:52
  • 1
    why viewWillAppear()? I am doing this in viewDidLoad and it works fine. – Adam Johns Jul 12 '15 at 13:29
  • 4
    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. – FractalDoctor Dec 21 '15 at 08:35
  • Yes, either you put your setting value in UINavigationController ViewDidLoad or you put these setting value in UIViewController ViewWillApear function, both work for me – Chauyan Jan 14 '17 at 09:57
  • 2
    Update for swift 4 - `NSForegroundColorAttributeName` should now be `NSAttributedStringKey.foregroundColor` – Alan Scarpa Mar 30 '18 at 01:22
  • **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 – Sylvan D Ash Apr 11 '18 at 13:46
  • 5
    Update for Swift 4.2: `NSAttributedString.Key.foregroundColor` instead of `NSForegroundColorAttributeName` – Stephane Paquet Jan 02 '19 at 01:05
  • 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 Oct 22 '20 at 11:57
82

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.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
Alex
  • 5,298
  • 4
  • 29
  • 34
  • How do I override the NavigationController to use these methods? – AG1 Sep 24 '14 at 04:54
  • You don't override the NavigationController, you just modify it in the ViewDidLoad function. I updated my answer above. – Alex Sep 24 '14 at 04:55
  • 1
    So a developer just has to add NavigationController.swift to the workspace? If so that is neat! – AG1 Nov 07 '14 at 03:16
  • 5
    @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. – kakubei Dec 18 '14 at 12:58
  • 2
    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. – Markymark Dec 03 '16 at 05:46
38

Swift 5

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

Swift 4

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
Flower
  • 761
  • 7
  • 4
  • 2
    I get: Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor' – Ahmadreza Dec 10 '17 at 12:39
  • Please use below code for latest version xcode 10 and swift 4.2 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] – Bhavsang Jam Dec 05 '18 at 10:55
17

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
Nyakiba
  • 862
  • 8
  • 18
  • 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. – user3163404 Aug 24 '15 at 16:55
  • 1
    I think you also have to set the color in your new view controller. – Niko Klausnitzer Aug 28 '15 at 08:07
15

To do this job in storyboard (Interface Builder Inspector)

With help of IBDesignable, we can add more options to Interface Builder Inspector for UINavigationController and tweak them on storyboard. First, add the following code to your project.

@IBDesignable extension UINavigationController {
    @IBInspectable var barTintColor: UIColor? {
        set {
            navigationBar.barTintColor = newValue
        }
        get {
            guard  let color = navigationBar.barTintColor else { return nil }
            return color
        }
    }

    @IBInspectable var tintColor: UIColor? {
        set {
            navigationBar.tintColor = newValue
        }
        get {
            guard  let color = navigationBar.tintColor else { return nil }
            return color
        }
    }

    @IBInspectable var titleColor: UIColor? {
        set {
            guard let color = newValue else { return }
            navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
        }
        get {
            return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
        }
    }
}

Then simply set the attributes for UINavigationController on storyboard.

enter image description here

Fangming
  • 24,551
  • 6
  • 100
  • 90
8

In Swift5 and Xcode 10

self.navigationItem.title = "your name"
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
kuzdu
  • 7,124
  • 1
  • 51
  • 69
Naresh
  • 16,698
  • 6
  • 112
  • 113
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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. – 31piy Jun 11 '18 at 11:37
7

If you want to set the tint color and bar color for the entire app, the following code can be added to AppDelegate.swift in

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

    var navigationBarAppearace = UINavigationBar.appearance()

    navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
    navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    return true
`

Navigation barTintColor and tintColor is set

VirajP
  • 91
  • 1
  • 5
6

Updated with swift 4

override func viewDidLoad() {
    super.viewDidLoad()
        self.navigationController?.navigationBar.tintColor = UIColor.blue
        self.navigationController?.navigationBar.barStyle = UIBarStyle.black
}
Raj Joshi
  • 2,669
  • 2
  • 30
  • 37
5

Swift 5.1

Only copy and Paste in ViewDidLoad() and Change its and size as your need. Before copy and paste add Navigation Bar on top of the Screen.

navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "TitilliumWeb-Bold.ttf", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]

If it not work then you can try for only change its text color

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Raksha.
  • 479
  • 6
  • 6
4

Swift 4.2 version of Albert's answer-

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 = [.foregroundColor : UIColor.white]
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
  • I really liked how you added this answer. .barTintColor isn't an option now. Do you mean .backgroundColor? – Ben Mar 02 '19 at 18:08
4

Setting text color of navigation bar title to white in Swift version 4.2:

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Rawand Saeed
  • 795
  • 10
  • 13
3

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.barTintColor = UIColor.orange
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
Vladimir
  • 1,053
  • 10
  • 8
3

Swift 4.1

Add a func to viewDidLoad

override func viewDidLoad() {
  super.viewDidLoad()

  setup()
}   

In the setup() function add:

func setup() {

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationController?.navigationBar.barStyle = .blackOpaque
        navigationItem.title = "YOUR_TITLE_HERE"
        navigationController?.navigationBar.barTintColor = .black
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        navigationController?.navigationBar.largeTitleTextAttributes = attributes
    }
Patrick.Bellot
  • 303
  • 4
  • 10
1

For custom color to TitleText at NavigationBar, here a simple and short code for Swift 3:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

or

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName :UIColor.white]
Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
ifredy3
  • 29
  • 3
1

in Swift 4.2

var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.white
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
Imtee
  • 1,345
  • 13
  • 14
1

Swift up through Swift 3.2 (not Swift 4.0)

    self.navigationController?.navigationItem.largeTitleDisplayMode = .always
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

    // unconfirmed but I assume this works:
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    self.navigationController?.navigationBar.barStyle = UIBarStyle.black
bubbaspike
  • 101
  • 6
0

In Swift 3 this works:

navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46