简体   繁体   English

UINavigationBar不会设置标题文本属性

[英]UINavigationBar won't set title text attributes

Im trying to set my apps title text color to white, however when i try setting the titleTextAttributes nothing appears to work. 我试图将我的应用程序标题文本颜色设置为白色,但是当我尝试设置titleTextAttributes时,似乎没有任何作用。

I've got other appearance properties set such as background color and tint colour. 我还设置了其他外观属性,例如背景色和淡色。 But the NSAttributedString.Key.foregroundColor appears to not work correctly. 但是NSAttributedString.Key.foregroundColor似乎无法正常工作。 I've double check documentation to ensure its taking UIColor and still appears to be set up correctly with no results. 我已经仔细检查了文档,以确保其采用UIColor,并且仍然似乎设置正确,没有任何结果。 Below is my AppDelegate for reference. 以下是我的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]

Found the answer! 找到了答案! In case anyone comes across this. 万一有人碰到这个。

I was using the prefersLargeTitle = true navigation bar. 我正在使用preferredsLargeTitle = true导航栏。

In order to set the title text attributes for this style, you need the LargeTitleTextAttributes which is set exactly the same as a regular title attribute but with the largeTitleTextAttributes property instead eg: 为了设置这种样式的标题文本属性,您需要LargeTitleTextAttributes,它的设置与常规标题属性完全相同,但是具有largeTitleTextAttributes属性,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM