在swift 3开发时,我习惯写:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
将其放入AppDelegate会改变所有UINavbars'标题颜色为橙色。
现在我想对Swift 4和iOS 11做同样的事情。
答案 0 :(得分:13)
您可以通过在foregroundColor
上设置titleTextAttributes
属性来更改UINavigationBar标题颜色。
记录在案here:
titleTextAttributes属性指定的属性 显示栏的标题文字。你可以指定字体,文字颜色, 文本阴影颜色和文本中标题的文本阴影偏移量 使用font,foregroundColor和shadow的属性字典 钥匙,分别。
所以你这样做会有同样的效果:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
答案 1 :(得分:7)
Swift 4.1更改导航栏标题颜色
if #available(iOS 11.0, *) {
//To change iOS 11 navigationBar largeTitle color
UINavigationBar.appearance().prefersLargeTitles = true
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
} else {
// for default navigation bar title color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}
Swift 4.2+更改导航栏标题颜色
if #available(iOS 11.0, *) {
//To change iOS 11 navigationBar largeTitle color
UINavigationBar.appearance().prefersLargeTitles = true
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
} else {
// for default navigation bar title color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
答案 2 :(得分:4)
Swift 4
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]
答案 3 :(得分:1)
您可以使用此代码更改导航栏标题的颜色
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
答案 4 :(得分:1)
大型标题和常规标题的Swift 4实现:
extension UINavigationController {
func setTitleForgroundTitleColor(_ color: UIColor) {
self.navigationBar.titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
}
func setLargeTitleColor(_ color: UIColor) {
self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
}
func setAllTitleColor(_ color: UIColor) {
setTitleForgroundTitleColor(color)
setLargeTitleColor(color)
}
}
答案 5 :(得分:1)
应用程序代表迅捷4.2
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]
答案 6 :(得分:0)
Swift 4.2
fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
guard let input = input else { return nil }
return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}
用法
// Set Navigation bar Title color
UINavigationBar.appearance().titleTextAttributes = convertToOptionalNSAttributedStringKeyDictionary([NSAttributedString.Key.foregroundColor.rawValue : UIColor.white])
答案 7 :(得分:0)
对于Swift 5,如果有人像我一样四处张望:
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]