我正在开发一个简单的教堂应用程序。而且我已经启动了滑出式菜单,当我将它设置为新字体时,我似乎无法更改导航栏字体的颜色。当我没有为字体更改编写的代码行时,颜色会发生变化。但是,一旦我添加了更改字体的代码行,颜色就会恢复为默认颜色。我在下面提供了我的代码,并附有图片。我试过将这个函数放在 AppDelegate 中(我看到多个论坛都说这是诀窍),但似乎没有任何效果。
当我这样做时,颜色会改变。
func customizeNavBar() {
navigationController?.navigationBar.tintColor = UIColor(colorLiteralRed: 255/255, green: 255/255, blue: 255/255, alpha: 1)
navigationController?.navigationBar.barTintColor = UIColor(colorLiteralRed: 0/255, green: 0/255, blue: 0/255, alpha: 1)
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
}
当我添加 NSFontAttributeName 代码行时,字体类型发生变化,但颜色变回黑色。
func customizeNavBar() {
navigationController?.navigationBar.tintColor = UIColor(colorLiteralRed: 255/255, green: 255/255, blue: 255/255, alpha: 1)
navigationController?.navigationBar.barTintColor = UIColor(colorLiteralRed: 0/255, green: 0/255, blue: 0/255, alpha: 1)
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 21)!]
}
最佳答案
您已将前景色替换为第二个属性。你需要将它们结合起来
试试这个
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 21)!]
关于当我设置新字体时,iOS 导航栏不改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746809/