Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 389
Sanf0rd Avatar asked Jul 21 '17 00:07

Sanf0rd


People also ask

How do I change the navigation bar title font in Swift?

let navigation = UINavigationBar. appearance() let navigationFont = UIFont(name: "Custom_Font_Name", size: 20) let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default navigation. titleTextAttributes = [NSAttributedStringKey. foregroundColor: UIColor.

How do I change the title of the navigation bar in Swift storyboard?

1: Select the "navigation item" from the document outline pane. 2: Open the attributes-inspector. 3: Select the title field and write the title here instead.


1 Answers

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]
like image 122
mtrevia Avatar answered Sep 20 '22 05:09

mtrevia