如何更改导航栏标题的颜色?

时间:2018-08-02 16:34:00

标签: ios swift uiviewcontroller uinavigationcontroller

我有一个简单的导航栏,以及我通过情节提要设置的标题。我为标题设置了白色。

enter image description here

然后我增加了尺寸。

navigationController?.navigationBar.prefersLargeTitles = true

enter image description here

这会自动更改标题的颜色/字体和所有属性。

我尝试了几种方法(通常更改所有属性),但是在我增加尺寸后,似乎没有一种方法可以解决此问题。

在这种情况下如何自定义标题?

注意* :我要询问有关特定NavVC / VC标题的颜色。不在所有VC的AppDelegate中。

答案:请遵循: Changing the text color of a navigation bar title when "prefersLargeTitles" is set to true

3 个答案:

答案 0 :(得分:3)

使用以下代码更改颜色

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

如果另一个不起作用,请尝试这样;

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

        UINavigationBar.appearance().prefersLargeTitles = true
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]

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

    }

答案 1 :(得分:1)

请将此代码添加到viewDidLoad()中,以更改标题的颜色/字体。

 if #available(iOS 11.0, *) {
            UINavigationBar.appearance().largeTitleTextAttributes =
                [NSAttributedStringKey.foregroundColor: UIColor.green,
                 NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 20)]
        } else {
            // Fallback on earlier versions
        }

答案 2 :(得分:1)

简单的事情转到情节提要,在Navigation Controller场景中选择Navigation Bar

enter image description here

然后从属性检查器中设置正常标题和大标题的颜色,如下所示:

enter image description here

输出为:

enter image description here

相关问题