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

标签 ios colors uinavigationbar swift3

我正在尝试在我的 AppDelegate.swiftUINavigationBar 中设置标题颜色,就像这样-

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
}

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

最佳答案

您正在将您最初使用颜色设置的 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/

相关文章:

ios - 卸载应用程序后注销用户 - Firebase

ios - 隐藏导航栏但显示带背景的状态栏

ios - 为所有 UINavigationBar 设置颜色

ios - 点击导航栏上的返回项时更新表格 View 数据源

ios - NSDate - 将日期转换为 GMT

android - react native 系统声音/铃声

android/iphone点击调用html

ios - 颜色计算 : increase alpha but maintain the same color appearance over a white background

html - 如何将控制台应用程序(如 MSBuild 或 PowerShell)的彩色输出转换为保留颜色的 HTML 格式?

delphi - 如何以编程方式更改标签的字体颜色