How is it I can animate the change in bar tint color of a UINavigationBar but not a UITabBar?

3.7k views Asked by At

I'm implementing a theme in my application and I ran into a weird bug (feature?). For some odd reason, I can't use UIView.animate in my custom UITabBarController class to animate a change in color of my UITabBar but the same exact code works perfectly in the custom class of my UINavigationController.

Am I missing something? Is there something else I can use to animate the color change? I've scoured the Apple Documents but found nothing.

Here is the code I'm using in both cases:

class customNavigationController: UINavigationController {
    @IBOutlet weak var navBar = ThemeManager.navigationbar

    func dusk(notification: NSNotification) {
        UIView.animateWithDuration(1, animations: {
            self.navBar?.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1)
            self.navBar?.barStyle = UIBarStyle.Black

        })
    }
}

And:

class customTabController: UITabBarController {
    @IBOutlet weak var tab = ThemeManager.tabbar

    func dusk(notification: NSNotification) {
        UIView.animateWithDuration(1, animations: {
            self.tab?.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1)
            self.tab?.barStyle = UIBarStyle.Black
        })
    }
}
2

There are 2 answers

0
Fallah On

Well to change color on the tab bar personally I use a whole different image with different colors enter image description here

As you can see my selected image is my second image. Right now it won't show that image unless I add it to my "User Defined Runtime Attributes"

enter image description here

Set Keypath to "selectedImage", the type to "Image" and the value to your image that you'd like to display when presented. I hope this answers your question.

6
Allan Weir On

You can use UIView.transitionWithView to fade between the two states

UIView.transitionWithView(self.tab!, duration: 1.0, options: .BeginFromCurrentState | .TransitionCrossDissolve, animations: { () -> Void in
        self.tab!.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1)
        self.tab!.barStyle = UIBarStyle.Black
    }, completion: nil)