Search code examples
iosswiftuikituinavigationbarswift4

How can I change the NavBar title text color in swift 4?


When developing in swift 3 I was used to write:

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

putting this in AppDelegate would change all the UINavbars' title color to orange.

Now I want to do the same with Swift 4 and iOS 11.


Solution

  • You can change the UINavigationBar title color by setting the foregroundColor property on titleTextAttributes.

    As documented here:

    The titleTextAttributes property specifies the attributes for displaying the bar’s title text. You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary using the font , foregroundColor , and shadow keys, respectively.

    So you will have the same effect doing this:

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