如何在Swift 4中更改导航栏标题文本颜色?

7

在使用Swift 3开发时,我习惯于编写以下代码:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]

将以下代码放在AppDelegate中,可以将所有UINavbars的标题颜色更改为橙色。
现在我想在Swift 4和iOS 11中实现相同的效果。
8个回答

16

您可以通过在 titleTextAttributes 上设置 foregroundColor 属性来更改 UINavigationBar 标题的颜色。

如此文档所述:这里:

titleTextAttributes 属性指定用于显示栏标题文本的属性。您可以使用文本属性字典中的 fontforegroundColorshadow 键,分别指定标题的字体、文本颜色、文本阴影颜色和文本阴影偏移量。

因此,这样做将产生相同的效果:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.orange]

9

Swift 4.1更改导航栏标题颜色

    if #available(iOS 11.0, *) {
        //To change iOS 11 navigationBar largeTitle color

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    } else {
        // for default navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }

Swift 4.2+ 更改导航栏标题颜色

    if #available(iOS 11.0, *) {
        //To change iOS 11 navigationBar largeTitle color

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    } else {
        // for default navigation bar title color
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    }

NSAttributedStringKey 已更新为 NSAttributedString.Key。 - user1872384

5

应用程序代理 Swift 4.2

 UINavigationBar.appearance().barTintColor = UIColor.white
 UINavigationBar.appearance().tintColor = UIColor(hex: 0xED6E19)
 UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(hex: 0xED6E19)]

4

Swift 4

    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.orange]

2

Swift 4实现大标题和普通标题:

extension UINavigationController {
    func setTitleForgroundTitleColor(_ color: UIColor) {
        self.navigationBar.titleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
    }

    func setLargeTitleColor(_ color: UIColor) {
        self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): color]
    }

    func setAllTitleColor(_ color: UIColor) {
        setTitleForgroundTitleColor(color)
        setLargeTitleColor(color)
    }
}

1
你可以使用以下代码更改导航栏标题的颜色
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

0

Swift 4.2

fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
        guard let input = input else { return nil }
        return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}

用法

   // Set Navigation bar Title color

UINavigationBar.appearance().titleTextAttributes = convertToOptionalNSAttributedStringKeyDictionary([NSAttributedString.Key.foregroundColor.rawValue : UIColor.white])

0

对于 Swift 5,如果有人像我一样在寻找:

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(displayP3Red: 84/255, green: 93/255, blue: 118/255, alpha: 1)]

对于那些在iOS15上着陆的人,您需要将上述值添加到“UINavigationBarAppearance”中,并将其设置为导航栏。 “UINavigationBarAppearance”是在iOS13中引入的(我没有测试13或14)。 - Gavin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接