user4992124
user4992124

Reputation: 1594

Change iOS 11 large title color

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?

Upvotes: 12

Views: 14451

Answers (7)

Mahi Al Jawad
Mahi Al Jawad

Reputation: 1050

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

Upvotes: 2

kobeli
kobeli

Reputation: 1

  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
            }

Upvotes: 0

bubbaspike
bubbaspike

Reputation: 111

Swift up through Swift 3.2 (not Swift 4+)

        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

Upvotes: 1

Juanes30
Juanes30

Reputation: 2566

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]

Upvotes: 2

Vivek
Vivek

Reputation: 5223

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
}

Upvotes: 2

Kevin Furman
Kevin Furman

Reputation: 171

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!

Upvotes: 17

David Knight
David Knight

Reputation: 761

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

Upvotes: 23

Related Questions