80

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.

0

11 Answers 11

206

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
}
4
  • you rock, man. But what's the different, when I coded this on viewDidLoad:navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red] it doesn't work
    – Edward
    Apr 30, 2017 at 12:47
  • thanks @熊天宇 , I can't really say why it didn't work when you coded in viewDidLoad until I see your full source code . By the way , I was successful while coded in viewDidLoad , have a look at my code override func viewDidLoad() { super.viewDidLoad() user = "holla" let textAttributes = [NSForegroundColorAttributeName:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes }
    – roy
    Apr 30, 2017 at 12:59
  • Works on tvOS to change the UIViewController title.
    – dar512
    Nov 6, 2018 at 22:53
  • If your navigation bar title uses custom font then this solution works. Using the answer "Keeping all the other attributes of the title" was what I needed.
    – Andrew
    Jun 16, 2021 at 20:35
45

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

0
45

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
2
  • 1
    Thanks, Working! Mar 26, 2020 at 16:50
  • 5
    anyone who came here because of iOS15, this is the correct answer :) I've added largeTitleTextAttributes and the title has changed.
    – emrcftci
    Sep 29, 2021 at 15:44
27

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]
10

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]
}
7

In Objective C:

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

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
}
2
  • So, should I set the username color??
    – Edward
    Apr 30, 2017 at 12:22
  • J.Koush thank you so much, I know this command:navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red] But it still doesn't change...
    – Edward
    Apr 30, 2017 at 12:41
4

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]
4

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

4

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

2
  • i have code to set using UINavigationBarAppearance() but adding the below code along with appearance object worked UINavigationBar.appearance().tintColor = UIColor.white Dec 23, 2021 at 21:58
  • Happy that's working with you @kingkongCoder
    – Menaim
    Dec 24, 2021 at 12:21
2

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() {
       // change title
       navigationItem.title = "Profile"
       navigationController?.navigationBar.prefersLargeTitles = true

       // change title color
       let textChangeColor = [NSAttributedString.Key.foregroundColor: UIColor.black]
       navigationController?.navigationBar.titleTextAttributes = textChangeColor
       navigationController?.navigationBar.largeTitleTextAttributes = textChangeColor

       // configure right bar button
       navigationItem.rightBarButtonItem?.tintColor = .white
       navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
       navigationItem.rightBarButtonItem?.tintColor = .white
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.