代碼如下, 當(dāng)你修改完約束之后添加動(dòng)畫之前, 你需要事先調(diào)用一次self.view.layoutIfNeeded
.
它能確保在動(dòng)畫過程之中只有你修改的約束,會(huì)被加上動(dòng)畫效果.
否者其他view發(fā)生變化的時(shí)候也會(huì)被這個(gè)動(dòng)畫影響.
...
self.view.layoutIfNeeded()
topInfosViewHeight.constant = 102
UIView.animateWithSpring({
self.view.layoutIfNeeded()
})
...
extension UIView {
class func animateWithSpring(animate: ()->Void) {
animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: .LayoutSubviews, animations: animate, completion: nil)
}
}
Referencer: Best practice for modifying storyboard auto layout constraints in code