UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName不起作用

Geo*_*e B 0 colors uinavigationbar ios swift3

我试图在我的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
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.结果看起来像这个 为什么这不起作用?谢谢!

par*_*par 6

titleTextAttributes将最初使用颜色设置的值覆盖为仅包含字体的新值.

您应该组合您的属性,然后立即设置它们:

编辑:斯威夫特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
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!

let attributes: [String: AnyObject] = [
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: color
]

UINavigationBar.appearance().titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)