Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change navigationitem title color

Tags:

ios

swift

I think all day to change the navigation Bar title color, but it doesn't work. this is my code:

var user: User? {     didSet {         navigationItem.title = user?.name           observeMessages()     } } 

I use didSet to show the title on the navigation title.

like image 718
Edward Avatar asked Apr 30 '17 11:04

Edward


People also ask

How do I change the color of my navigation bar text?

The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color. navbar-dark: This class will set the color of the text to light.

How do I use the navigation bar in Xcode?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.


2 Answers

Add this in your code . .

let textAttributes = [NSForegroundColorAttributeName:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes 

SWIFT 4:

let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes 

SWIFT 4.2+:

let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes 

Keeping all the other attributes of the title: If you just want change the color you could do like this:

if var textAttributes = navigationController?.navigationBar.titleTextAttributes {     textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red     navigationController?.navigationBar.titleTextAttributes = textAttributes } 
like image 180
roy Avatar answered Sep 27 '22 21:09

roy


The title color of Navigation Bar can be changed in Storyboard.

Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu.


enter image description here

like image 20
Tzar Avatar answered Sep 27 '22 19:09

Tzar