64

How would I go about changing the color of the UINavigationBar in Swift?

Most things online say to do something like:

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

Which I translated to

let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()]
self.navigationController.navigationBartitleTextAttributes = titleDict
//self is referring to a UIViewController

But it doesn't work. I already changed the background and button colors, but the text color doesn't change. Any ideas?

0

11 Answers 11

102

Use NSForegroundColorAttributeName as key, not "NSForegroundColorAttributeName" string.

let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict
2
  • 6
    As per Swift 1.2 update, the syntax needs to be this let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController!.navigationBar.titleTextAttributes = titleDict as [NSObject : AnyObject] Commented Jun 3, 2015 at 14:08
  • 2
    As per Swift 2.2: let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController!.navigationBar.titleTextAttributes = titleDict as? [String : AnyObject] Commented May 12, 2016 at 21:39
38

You can also change all UINavigationController appearances in your app within the AppDelegate.swift file. Just put the following code within the application:didFinishLaunchingWithOptions function:

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = UIColor.YourNavigationButtonsColor()  // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.YourBackgroundColor()  // Bar's background color

navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.YourTitleColor()]  // Title's text color

Creds: Coderwall's Blog Post

1
  • 2
    Thank you for having comments next to the tint / bar tint color, that helped me realize what I was doing wrong, and thus ended the banging my head against a wall cycle. :D
    – D. Pratt
    Commented Nov 28, 2018 at 15:57
35

Swift 3+

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

Swift 4.0

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
1
  • Works, thanks! Unbelievably horrible though that you can't simply set an outlet or style in IB ... I find Nav Controllers to be hard enough to customize that I end up making my own top bar :/ Commented Dec 27, 2016 at 1:28
28

Swift 2.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
0
2
    //Nav Bar Title
    self.title = "WORK ORDER"
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
2

Swift 3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal)
2

Swift 4.x:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
2

Swift 4.2

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

I use like:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      let navigationBarAppearace = UINavigationBar.appearance()
      navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

     return true
   }
1

Swift 5.1:

    let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white]
    navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any]
0
    let titleDict = [NSForegroundColorAttributeName: UIColor.white]
    self.navigationController?.navigationBar.titleTextAttributes = titleDict

Not the answer you're looking for? Browse other questions tagged or ask your own question.