首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName不工作

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName不工作
EN

Stack Overflow用户
提问于 2016-11-11 19:56:06
回答 4查看 4.9K关注 0票数 2

我正试图在我的UINavigationBar中设置标题颜色,在我的AppDelegate.swift中,像这样-

代码语言:javascript
复制
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
}

但不起作用。结果就像

为什么这不管用?谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-11-11 19:59:45

您正在将最初使用颜色设置的titleTextAttributes值重写为仅包含字体的新值。

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

编辑: Swift 4

代码语言:javascript
复制
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

代码语言:javascript
复制
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!

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

UINavigationBar.appearance().titleTextAttributes = attributes
票数 9
EN

Stack Overflow用户

发布于 2018-01-23 14:30:11

Swift 4:

对于NavigationBar背景色:

代码语言:javascript
复制
UINavigationBar.appearance().barTintColor = UIColor(red: 50/255, green: 90/255, blue: 150/255, alpha: 1)

对于NavigationBar标题、颜色和字体:

代码语言:javascript
复制
let attrs = [
    NSAttributedStringKey.foregroundColor: UIColor.red,
    NSAttributedStringKey.font: UIFont(name: "Georgia-Bold", size: 24)!
]

UINavigationBar.appearance().titleTextAttributes = attrs

参考资料:这里

票数 2
EN

Stack Overflow用户

发布于 2019-02-25 13:07:42

代表Swift 4.2

代码语言:javascript
复制
let color = UIColor.white
let font = UIFont(name: "Pacifico", size: 24)!

let attributes = [
  NSAttributedString.Key.font: font,
  NSAttributedString.Key.foregroundColor: color
]

UINavigationBar.appearance().titleTextAttributes = attributes
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40555278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档