iOS10中的动画导航栏barTintColor更改无法正常工作

Tio*_*ois 10 ios swift2 ios10

我升级到XCode 8.0/iOS 10,现在我的导航栏的换色动画不再起作用,它直接改变颜色而没有任何动画.

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

谁知道如何解决这个问题?

Vas*_*ily 29

要在iOS10中为navigationBar的颜色更改设置动画,您需要layoutIfNeeded在动画块中设置颜色后调用.

示例代码:

UIView.animateWithDuration(0.5) { 
    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    self.navigationController?.navigationBar.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)

另外我想通知Apple 没有正式支持像barTintColor这样的属性中的动画,因此该方法可以随时中断.

如果在动画块期间在导航栏上调用-layoutIfNeeded,它应该更新其背景属性,但考虑到这些属性的性质,确实没有任何保证可以为它们中的任何一个设置动画.