在使用Swift 3开发时,我习惯于编写以下代码:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
将以下代码放在AppDelegate中,可以将所有UINavbars的标题颜色更改为橙色。
现在我想在Swift 4和iOS 11中实现相同的效果。
在使用Swift 3开发时,我习惯于编写以下代码:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
您可以通过在 titleTextAttributes
上设置 foregroundColor
属性来更改 UINavigationBar 标题的颜色。
如此文档所述:这里:
titleTextAttributes
属性指定用于显示栏标题文本的属性。您可以使用文本属性字典中的font
、foregroundColor
和shadow
键,分别指定标题的字体、文本颜色、文本阴影颜色和文本阴影偏移量。
因此,这样做将产生相同的效果:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
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]
}
应用程序代理 Swift 4.2
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]
Swift 4
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]
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)
}
}
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
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])
对于 Swift 5,如果有人像我一样在寻找:
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]