UINavigationBar - 更改标题颜色

UINavigationBar - change title color

我尝试更改导航栏标题的颜色并尝试了以下操作:

let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = UIColor.red
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
navigationBarAppearace.tintColor = UIColor.white

一切正常,但标题的颜色没有改变。它仍然是黑色的。我以为这条线会改变它

navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

这是错误的吗?

在你的控制器中试试这个,你必须为大标题和普通标题设置一次颜色。

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]
    navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]