how to change navigationitem title color

ios, swift

Solution

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
}

Problem

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.

Original source