Change iOS 11 large title color

旧时模样 提交于 2019-12-01 20:55:44

问题


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?


回答1:


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



回答2:


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!




回答3:


iOS 11

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
}



回答4:


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]



回答5:


Swift up through Swift 3.2 (not Swift 4+)

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


来源:https://stackoverflow.com/questions/44823284/change-ios-11-large-title-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!