简体   繁体   English

后退按钮文字颜色恢复为默认值

[英]Back button text colour reverts to default

I am having an issue in my app with a particular back button changing font colour for no apparent reason. 我的应用程序出现问题,没有明显的原因,特定的后退按钮更改了字体颜色。 I have been through all of my code and I can't see any reason for it, but when launching my app and segueing to the problem view controller, the text colour seems to be set to the default blue colour. 我已经遍历了所有代码,但看不到任何原因,但是当启动我的应用程序并选择到问题视图控制器时,文本颜色似乎设置为默认的蓝色。

If I tap to go to a different tab and then go back to this view controller, then the font returns to the correct colour (white) and this is despite not having any code in viewWillAppear/Disappear viewDidAppear/Disappear. 如果我点击以转到其他选项卡,然后返回此视图控制器,则字体将返回正确的颜色(白色),尽管在viewWillAppear / Disappear viewDidAppear / Disappear中没有任何代码。

The code I am using to set the navigation bar text colour is: 我用来设置导航栏文本颜色的代码是:

override func awakeFromNib() {
    var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
    self.navigationController?.navigationBar.titleTextAttributes = attributes
}

And this is in the view controller that is segued from when going to the view controller with the back button. 这是在使用后退按钮进入视图控制器时与之隔离的视图控制器中。 I have tried adding this code into viewWillAppear() but even this doesn't make a difference. 我尝试将这段代码添加到viewWillAppear()但是即使这样也没有任何区别。

Does anyone know why, on launch, my app doesn't set the back button text colour and requires a tab switch for it to update? 有谁知道为什么在启动时我的应用程序未设置后退按钮文本颜色,并且需要使用选项卡开关进行更新?

请尝试在视图将出现的方法中编写相同的代码。

I think this code will work 我认为这段代码会工作

self.navigationController?.navigationBar.barTintColor = UIColor.someColor() 
//This sets the background colour of the navigation bar

self.navigationController?.navigationBar.tintColor = UIColor.someColor()
// This sets the colour for navigation and bar button items 

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.blackColor()]
//This sets the attributed text for the title of the navigation bar

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
//And this sets the attributed text for the bar button item(s) on the navigation bar (So that includes the back button)

Call it in the viewWillAppear I hope this works :) 希望在viewWillAppear中调用它,我希望它能起作用:)

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

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