12

I'm using the new enlarged navigation bar titles in iOS 11. But I can't seem to be able to change the textColor.

I tried doing:

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

This didn't do anything. Any ideas?

user4992124
  • 1,574
  • 1
  • 17
  • 35
  • Possible duplicate of [Changing the text color of a navigation bar title when "prefersLargeTitles" is set to true](https://stackoverflow.com/questions/44619396/changing-the-text-color-of-a-navigation-bar-title-when-preferslargetitles-is-s) – Kqtr Feb 03 '18 at 14:08
  • There use to be a bug in Xcode, but it is now working. Complete working code here: https://stackoverflow.com/a/48598246/7698127 (not posting an answer here to avoid duplicate answers) – Kqtr Feb 03 '18 at 14:09

7 Answers7

23
self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; 
David Knight
  • 763
  • 6
  • 12
17

I think it's still a bug in Xcode 9 beta 6.

I found different "solutions" for it:

  1. It's possible to change the color of the title if you put this in the AppDelegate:
    if #available(iOS 11.0, *) {
      UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
    }
  1. Other way is to set the color in your Controller's viewDidLoad, but the secret to make it work is to set the font also:
    if #available(iOS 11.0, *) {            
      self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 31, weight: UIFont.Weight.bold) ]
    }

Hope it helps you.

Regards!

Kevin Furman
  • 171
  • 1
  • 5
2

iOS 11

enter image description here

Objective-C

if (@available(iOS 11.0, *)) {
    self.navigationController.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeAlways;
    self.navigationController.navigationBar.prefersLargeTitles = true;

// Change Color
    self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

} else {
    // Fallback on earlier versions
}
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40
2

Swift 4.2

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

with named color

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(named: "Teal") ?? UIColor.black]
Community
  • 1
  • 1
Juanes30
  • 2,398
  • 2
  • 24
  • 38
2

Swift 5.6.1

In my Swift 5.6.1 and iOS 15.6.1 the following code worked only.

Add the following codes in ViewDidLoad()

    let appearance = UINavigationBarAppearance(idiom: .phone)
    // Add the color you want in your title
    appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    // Add the color you want as your navigation bar color. Otherwise it shows White by default
    appearance.backgroundColor = .purple
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
1

Swift up through Swift 3.2 (not Swift 4+)

        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
bubbaspike
  • 101
  • 6
0
  let largeTitleTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.gray20, .font: UIFont.systemFont(ofSize: 24.0, weight: .bold)]
    if #available(iOS 15, *) {
                let navigationBar = navigationController.navigationBar
                let appearance = navigationBar.standardAppearance
                    appearance.largeTitleTextAttributes = largeTitleTextAttributes
                    navigationBar.standardAppearance = appearance
                    navigationBar.scrollEdgeAppearance = appearance
            } else {
                    navigationController.navigationBar.largeTitleTextAttributes = largeTitleTextAttributes
            }
kobeli
  • 1
  • 3
  • Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See [How to Answer](https://stackoverflow.com/help/how-to-answer). – Sfili_81 Nov 18 '21 at 07:43