Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate navigation bar barTintColor change in iOS10 not working

Tags:

ios

ios10

swift2

I upgraded to XCode 8.0 / iOS 10 and now the color change animation of my navigation bar is not working anymore, it changes the color directly without any animation.

UIView.animateWithDuration(0.2, animations: {
    self.navigationController?.navigationBar.barTintColor = currentSection.color!
})

Anyone knows how to fix this?

like image 409
Tiois Avatar asked Sep 15 '16 15:09

Tiois


1 Answers

To animate navigationBar’s color change in iOS10 you need to call layoutIfNeeded after setting color inside animation block.

Example code:

UIView.animateWithDuration(0.5) { 
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    self.navigationController?.navigationBar.layoutIfNeeded()
}

Also I want to inform that Apple doesn’t officialy support animations in such properties like barTintColor, so that method can break at any time.

If you call -layoutIfNeeded on the navigation bar during the animation block it should update its background properties, but given the nature of what these properties do, there really hasn't ever been any kind of guarantee that you could animate any of them.

like image 191
Vasily Avatar answered Oct 19 '22 23:10

Vasily