【问题标题】:UINavigationBar won't set title text attributesUINavigationBar 不会设置标题文本属性
【发布时间】:2019-01-15 21:22:43
【问题描述】:

我正在尝试将我的应用程序标题文本颜色设置为白色,但是当我尝试设置 titleTextAttributes 时似乎没有任何效果。

我设置了其他外观属性,例如背景颜色和色调。但是 NSAttributedString.Key.foregroundColor 似乎无法正常工作。我仔细检查了文档以确保它采用 UIColor 并且似乎仍然设置正确但没有结果。下面是我的 AppDelegate 供参考。

//Set the window as the visible view.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()


// Make the view controller default the homeController.
window?.rootViewController = UINavigationController(rootViewController: HomeController())

//Customise navBar appearance   
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.isTranslucent = false
navBarAppearance.barTintColor = .backgroundLightBlack
navBarAppearance.tintColor = .seaFoamBlue

//This is the line that doesnt work :( 
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

【问题讨论】:

  • 您需要在创建导航控制器之前设置外观代理。或者直接设置你创建导航控制器的属性。
  • 感谢 rmaddy,但这似乎没有奏效。我尝试在创建导航控制器之前直接进行设置,但都没有成功!还有什么建议吗?谢谢

标签: ios swift uinavigationbar appdelegate appearance


【解决方案1】:

找到答案了!以防万一有人遇到这种情况。

我使用的是 prefersLargeTitle = true 导航栏。

为了设置此样式的标题文本属性,您需要设置与常规标题属性完全相同但使用 largeTitleTextAttributes 属性设置的 LargeTitleTextAttributes,例如:

navBarAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多