How is it I can animate the change in bar tint col

2019-04-06 10:19发布

问题:

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
        })
    }
}

回答1:

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)


回答2:

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

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"

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.