当我设置新字体时,iOS 导航栏不改变颜色

标签 ios swift xcode

我正在开发一个简单的教堂应用程序。而且我已经启动了滑出式菜单,当我将它设置为新字体时,我似乎无法更改导航栏字体的颜色。当我没有为字体更改编写的代码行时,颜色会发生变化。但是,一旦我添加了更改字体的代码行,颜色就会恢复为默认颜色。我在下面提供了我的代码,并附有图片。我试过将这个函数放在 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]

}

enter image description here

当我添加 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)!]

}

enter image description here

最佳答案

您已将前景色替换为第二个属性。你需要将它们结合起来

试试这个

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 21)!]

关于当我设置新字体时,iOS 导航栏不改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746809/

相关文章:

ios - TestFlight 在 Xcode 5.1 中显示符号未找到错误

ios - Swift 中的 IBM Silverpop 推送集成

ios - 在 XCode 中导出文件中的所有警告

ios - 钥匙串(keychain)可以像类固醇上的 NSUserDefaults 一样用于存储 NSData 吗?

ios - 导入我的自定义模块/框架 Xcode Swift

ios - URLSession 不会在 header swift 4 中传递 'Authorization' 键

swift - 与时间相关的计算

swift - FileManager.contentsEqual 在比较复制的文件时返回 false

iphone - Xcode 4 - 在新的 Macintosh 安装上配置配置文件时出现 "Valid signing identity not found"错误

xcode项目打开没有.xcodeproj文件