简体   繁体   中英

Navigation Bar back button text colour reverts to default

I have some code in my app that is supposed to change the colour of the navigation bar font. The problem is that while I have been developing the code, I've been adding code to do this but I haven't been checking to see what parts I actually need. I am wondering if this is contributing to an issue I've been having.

Basically, occasionally (I haven't figured out exactly what the cause is) when I open the app, the colour of the back button text reverts to the default blue colour.

Here is my code:

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

    let appearanceTab = UITabBarItem.appearance()
    let appearanceNav = UINavigationBar.appearance()

    let attributesTabBar = [NSFontAttributeName:UIFont(name: "Avenir", size: 11)]
    appearanceTab.setTitleTextAttributes(attributesTabBar, forState: .Normal)

    appearanceNav.titleTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir", size: 25)]
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.barTintColor = UIColorFromRGB(0x009051)
    UINavigationBar.appearance().barTintColor = UIColorFromRGB(0x009051)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UITabBar.appearance().tintColor = UIColor.newBlueColor()
}

I've tried removing different parts of the code, but I haven't been able to identify the issue. Any ideas?

Add following code to didFinishLaunchingWithOptions function in AppDelegate.swift

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade

// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]

I gave a solution someone else once, I saved the code. Play with it this way some.

var attributes = [NSForegroundColorAttributeName: UIColor.greenColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 30)]
        self.navigationController?.navigationBar.titleTextAttributes = attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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