UINavigationBar Text Color in Swift

TextColorsNavigationUinavigationbarSwift

Text Problem Overview


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?

Text Solutions


Solution 1 - Text

Use NSForegroundColorAttributeName as key, not "NSForegroundColorAttributeName" string.

let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict

Solution 2 - Text

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

Solution 3 - Text

Swift 3+

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

Swift 4.0

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

Solution 4 - Text

Swift 2.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

Solution 5 - Text

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

Solution 6 - Text

Swift 3

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

Solution 7 - Text

Swift 4.x:

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

Solution 8 - Text

Swift 4.2

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

Solution 9 - Text

I use like:

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

Solution 10 - Text

Swift 5.1:

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

Solution 11 - Text

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncclloydView Question on Stackoverflow
Solution 1 - TextKreiriView Answer on Stackoverflow
Solution 2 - TextLouie BertoncinView Answer on Stackoverflow
Solution 3 - TextDasogaView Answer on Stackoverflow
Solution 4 - TextJasonHView Answer on Stackoverflow
Solution 5 - TextAlvin GeorgeView Answer on Stackoverflow
Solution 6 - TextzombieView Answer on Stackoverflow
Solution 7 - TextHemangView Answer on Stackoverflow
Solution 8 - TextrbaldwinView Answer on Stackoverflow
Solution 9 - TextMannam BrahmamView Answer on Stackoverflow
Solution 10 - TextIceManView Answer on Stackoverflow
Solution 11 - TextNubaslonView Answer on Stackoverflow