UINavigationBar不会设置标题文本属性

时间:2019-01-15 21:22:44

标签: ios swift uinavigationbar appdelegate appearance

我正在尝试将我的应用程序标题文本颜色设置为白色,但是当我尝试设置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]

1 个答案:

答案 0 :(得分:0)

找到了答案!万一有人碰到这个。

我正在使用preferredsLargeTitle = true导航栏。

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

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