我正在尝试在我的 AppDelegate.swift
的 UINavigationBar
中设置标题颜色,就像这样-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = UIColor(red: 26.0/255.0, green: 188.0/255.0, blue: 156.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Pacifico", size: 24)!]
// Turquoise color rgba(26, 188, 156,1.0)
return true
}
最佳答案
您正在将您最初使用颜色设置的 titleTextAttributes
值覆盖为仅包含字体的新值。
您应该组合您的属性,然后立即设置它们:
编辑:Swift 4
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes: [NSAttributedStringKey: AnyObject] = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: color
]
UINavigationBar.appearance().titleTextAttributes = attributes
swift 3
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes: [String: AnyObject] = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
]
UINavigationBar.appearance().titleTextAttributes = attributes
关于ios - UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555278/