2

I have different bar tint colours for different view controllers. Some View Controllers' bar tint colour requires the title text colour to be white and other View Controllers' bar tint colour requires it to be black. Now the code (called in the viewWillAppear method) I'm using for the view controllers with the white navigation bars is:

    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.blackColor()]

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)

The code (called in the viewWillAppear method) I am using for the darker navigation bars is:

    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]

     UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: UIControlState.Normal)

Now the problem is, my first two view controllers have white navigation bars, and my third view controller has a darker navigation bar.

Moving from the first to the second view controller, the back button appears normally, and is black. Moving from the second to third view controller the back button appears normally once again, and is white.

But after I press the back button on the third view controller and come to the second view controller, the back button remains white and is illegible (which I don't want). It turns black only when I tap on it; you can't see it before tapping on the top left corner. How do I make it black?

I've tried calling these methods in the viewDidLoad, the viewDidAppear and the viewWillAppear functions, but it just doesn't solve the problem.

I need help resolving this irritating glitch. Thanks.

Krish Wadhwana
  • 1,544
  • 2
  • 12
  • 24
Dieblitzen
  • 544
  • 4
  • 22

1 Answers1

0

Just set all the things directly without appearance proxy. Appearance proxy sets values for the whole app and it should be configured on early stages of app running. The best place for this is application:didFinishLaunchingWithOptions: of AppDelegate class. You could do it like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    configureAppearance()
    return true
}

func configureAppearance() {

    // Configure for all UIBarButtonItems

    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)


    // Configure for your view controller(s) where black button has to be

    UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([YourViewControllerWithBlackColorBtn.self]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
}
Krish Wadhwana
  • 1,544
  • 2
  • 12
  • 24
kirander
  • 2,202
  • 20
  • 18
  • So do I delete the appearance proxy? – Dieblitzen Sep 24 '15 at 13:21
  • Yes. Just delete the appearance() and try. – kirander Sep 24 '15 at 13:30
  • It gives me an error, "extra argument ForState in call". Could you tell me what the syntax is without the appearance()? Thanks @kirander – Dieblitzen Sep 24 '15 at 13:39
  • UIBarButtonItem.setTitleTextAttributes(...) – kirander Sep 24 '15 at 14:24
  • Its showing an error saying "Cannot convert value of type '[String: NSObject]' to expected argument type 'UIBarItem'" – Dieblitzen Sep 24 '15 at 14:30
  • Oh, sorry. That method should set properties for actual object of UIBarButtonItem. What class name should have white color? – kirander Sep 24 '15 at 14:40
  • Class name as in? I'm trying to set the colour of the back button text to black in this case. But I guess the method to set it to white will work fine – Dieblitzen Sep 24 '15 at 15:31
  • It's still not working... :( The glitch remains. If I travel back from a View Controller with a navigation bar that has a black back button, the back button remains black. But the back button remains white when I travel back from a View Controller that had a white back button @kirander – Dieblitzen Sep 24 '15 at 17:45
  • Double check you correctly configured nav bars and buttons. – kirander Sep 24 '15 at 18:15
  • Hi I double checked everything. But get this; I ran the app on my iPhone, and when I swipe to go back, the back button will turn black, like it's supposed to! But when I press the back button, it doesn't work (doesn't turn black). Is there any reason why this should happen? @kirander – Dieblitzen Sep 25 '15 at 05:21
  • Color of the buttons on navigation bar can be changed through navigationBar.tintColor property. Configure every controller where you need different color. – kirander Sep 25 '15 at 06:43
  • That's what I've done and been doing. It doesn't help @kirander – Dieblitzen Sep 25 '15 at 08:15