我正试图在我的UINavigationBar中设置标题颜色,在我的AppDelegate.swift中,像这样-
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
}但不起作用。结果就像

为什么这不管用?谢谢!
发布于 2016-11-11 19:59:45
您正在将最初使用颜色设置的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 = attributesSwift 3
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes: [String: AnyObject] = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
]
UINavigationBar.appearance().titleTextAttributes = attributes发布于 2018-01-23 14:30:11
Swift 4:
对于NavigationBar背景色:
UINavigationBar.appearance().barTintColor = UIColor(red: 50/255, green: 90/255, blue: 150/255, alpha: 1)对于NavigationBar标题、颜色和字体:
let attrs = [
NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "Georgia-Bold", size: 24)!
]
UINavigationBar.appearance().titleTextAttributes = attrs参考资料:这里
发布于 2019-02-25 13:07:42
代表Swift 4.2
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!
let attributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color
]
UINavigationBar.appearance().titleTextAttributes = attributeshttps://stackoverflow.com/questions/40555278
复制相似问题