When a user does some action, I need to pull a UICollectionView
from the bottom up to a certain height. Since that new state is totally optional, the collection
I've been in this problem for around 3 days and finally got a solution. Simply I added the animation block to be called after a very tiny timer
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7 , options: .CurveEaseInOut, animations: {
self.view.layoutIfNeeded()
}){ _ in}
will be
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.changeConstraint()
}
private func changeConstraint(){
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7 , options: .CurveEaseInOut, animations: {
self.view.layoutIfNeeded()
}){ _ in}
}
See the magic :)
Expanding from the top left corner is typically a symptom of calling layoutIfNeeded
in an animation block when the original view has never been laid out. You're basically animating the initial layout pass, where all subviews have started at CGRectZero.
To solve it you need two things:
layoutIfNeeded
on your view before you edit the constraint, then call it again in the animation block after you've made the change. This way you're only animating the change you've specified to the constraint, rather than the entire layout.