how to change navigationitem title color

IosSwift

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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
}

Solution 2 - Ios

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

Solution 3 - Ios

Solution for iOS 13

To customize the appearance of a navigation bar you need to use UINavigationBarAppearance:

let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]

navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance

Solution 4 - Ios

Swift 4

set this first

navigationController?.navigationBar.barStyle = .default

then one of those should work

navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]

or

navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]

Swift 5

navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

or

navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

Solution 5 - Ios

For iOS 13 you have to change the color in the appearance property and for older iOS versions you can do it directly in the navigation bar property.

if #available(iOS 13.0, *) {
    navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
} else {
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

Solution 6 - Ios

in objetive c:

[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
        [UIColor blackColor], NSForegroundColorAttributeName, 
           [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];

Solution 7 - Ios

The didSet is called if your set the user, maybe you're setting the user's name variable and expecting the program to enter didSet. try setting the user.

And if you want to change the color of the text when the navigation title is changed to the name of the user just call this code.

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

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

override func viewDidLoad() {
    super.viewDidLoad()

    let newUser = User()
    newUser.name = "John Doe"
    user = newUser
}

Solution 8 - Ios

If you set your navigation bar's title to prefer large titles, like so:

navigationBar.prefersLargeTitles = true

then you need to use the largeTitleTextAttributes property and not the titleTextAttributes property. If you set your nav title to be a large title, the titleTextAttribute is not the correct property to use. Use the largeTitleTextAttributes property, like so:

navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

Solution 9 - Ios

Swift 5/Xcode 11

Write this code as extension from UINavigationBar

extension UINavigationBar {
    func customNavigationBar() {
        // color for button images, indicators and etc. 
        self.tintColor = UIColor.Custom.mainAppColor

        // color for background of navigation bar
        // but if you use larget titles, then in viewDidLoad must write
        // navigationController?.view.backgroundColor = // your color
        self.barTintColor = .white
        self.isTranslucent = false

        // for larget titles 
        self.prefersLargeTitles = true

        // color for large title label
        self.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]

        // color for standard title label 
        self.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]

        // remove bottom line/shadow
        self.setBackgroundImage(UIImage(), for: .default)
        self.shadowImage = UIImage()
    }
}

then in AppDelegate.swift call UINavigationBar.appearance().customNavigationBar() in didFinishLaunchingWithOptions function

Solution 10 - Ios

you can just add this line of code in your AppDelegate in the func

didFinishLaunchingWithOptions

Code you will add to change title color:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]

you can add this line as well if you wanna change the backButton color

  UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)

Finally you can add this line to change the background color:

UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)

Note: You can change the values dependent on the color you want and have a happy coding day

Solution 11 - Ios

Swift 4

create project and test this with ViewController easy to use
import UIKit
class ProfileViewController: UIViewController {
      override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationBar()
          
          }
        func configureNavigationBar() {
            navigationItem.title = "Profile"
   let textChangeColor =[NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
              navigationItem.rightBarButtonItem?.tintColor = .white
            navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
              navigationController?.navigationBar.prefersLargeTitles = true
            navigationItem.rightBarButtonItem?.tintColor = .white
                    }
}

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
QuestionEdwardView Question on Stackoverflow
Solution 1 - IosroyView Answer on Stackoverflow
Solution 2 - IosTzarView Answer on Stackoverflow
Solution 3 - IosM RezaView Answer on Stackoverflow
Solution 4 - IosChaosTongView Answer on Stackoverflow
Solution 5 - IosChuy47View Answer on Stackoverflow
Solution 6 - IosAdrianaView Answer on Stackoverflow
Solution 7 - IosJ. KoushView Answer on Stackoverflow
Solution 8 - IosLylaakView Answer on Stackoverflow
Solution 9 - IosJack DanielView Answer on Stackoverflow
Solution 10 - IosMenaimView Answer on Stackoverflow
Solution 11 - Iosislam XDeveloperView Answer on Stackoverflow