Reputation: 25
I have a problem with the animation of a view after changing it's height constraint. In the screenshot, you can see it's initial value of 120.0. The animation works but the constraint update from my second view (the blue one) happens directly and not during the animation. This means that second view jumps to the top directly. With the following code, I will animate the change of the height constraint:
UIView.animate(withDuration: 3.0, animations: {
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
self.layoutIfNeeded()
})
Does anybody know why?
Upvotes: 2
Views: 1321
Reputation: 31
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
UIView.animate(withDuration: 3.0, animations: {
self.layoutIfNeeded()
})
It should be like this.
Upvotes: 3
Reputation: 526
You need to call self.layoutIfNeeded()
before and after updating constraint constant. Change your code to :
self.layoutIfNeeded()
UIView.animate(withDuration: 3.0, animations: {
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
self.layoutIfNeeded()
})
Upvotes: 1
Reputation: 4174
For animating constraint changes, you need to write code like below to work.
self.heightConstraint?.constant = 0.0
self.myLabel.alpha = 0.0
UIView.animate(withDuration: 5) {
self.layoutIfNeeded()
}
Upvotes: 1