如何在 swift 4 中更改 NavBar 标题文本颜色?
How can I change the NavBar title text color in swift 4?
在swift3开发的时候我习惯这样写:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
将其放入 AppDelegate 会将所有 UINavbar 的标题颜色更改为橙色。
现在我想对 Swift 4 和 iOS 11 做同样的事情。
您可以通过在 titleTextAttributes
上设置 foregroundColor
属性 来更改 UINavigationBar 标题颜色。
如文档所述here:
The titleTextAttributes property specifies the attributes for
displaying the bar’s title text. You can specify the font, text color,
text shadow color, and text shadow offset for the title in the text
attributes dictionary using the font , foregroundColor , and shadow
keys, respectively.
这样你会得到同样的效果:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
Swift 4
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: 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]
}
您可以使用此代码更改导航栏标题颜色
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
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)
}
}
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 4.2
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]
对于 Swift 5 如果有人像我一样四处张望:
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]
在swift3开发的时候我习惯这样写:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
将其放入 AppDelegate 会将所有 UINavbar 的标题颜色更改为橙色。
现在我想对 Swift 4 和 iOS 11 做同样的事情。
您可以通过在 titleTextAttributes
上设置 foregroundColor
属性 来更改 UINavigationBar 标题颜色。
如文档所述here:
The titleTextAttributes property specifies the attributes for displaying the bar’s title text. You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary using the font , foregroundColor , and shadow keys, respectively.
这样你会得到同样的效果:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]
Swift 4
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: 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]
}
您可以使用此代码更改导航栏标题颜色
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
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)
}
}
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 4.2
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]
对于 Swift 5 如果有人像我一样四处张望:
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]