更改iOS 11大标题颜色

use*_*124 4 objective-c uinavigationbar ios

我正在iOS 11中使用新的放大导航栏标题.但我似乎无法更改textColor.

我试过做:

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
Run Code Online (Sandbox Code Playgroud)

这没有做任何事情.有任何想法吗?

Dav*_*ght 20

self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; 
Run Code Online (Sandbox Code Playgroud)

  • 必须已过时,自 2019 年 1 月 6 日起在 Swift 4 中不起作用。 (2认同)

小智 8

我认为它仍然是Xcode 9 beta 6中的一个错误.

我找到了不同的"解决方案":

  1. 如果你把它放在AppDelegate中,可以改变标题的颜色:
    if #available(iOS 11.0, *) {
      UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
    }
  1. 其他方法是在Controller的viewDidLoad中设置颜色,但使其工作的秘诀是设置字体:
    if #available(iOS 11.0, *) {            
      self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 31, weight: UIFont.Weight.bold) ]
    }

希望它能帮到你.

问候!