Animate navigation bar barTintColor change in iOS10 not working

后端 未结 2 2025
梦谈多话
梦谈多话 2021-02-20 07:31

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.

<         


        
2条回答
  •  逝去的感伤
    2021-02-20 08:01

    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.

提交回复
热议问题