Reputation: 35
I am trying to animate the height change for a collection view, but I can't figure out how to get the animation to work. The height changes correctly, but it happens immediately, not animated.
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: {
self.collectionViewHeight.constant = 0
}
I tried searching for solutions, but couldn't find anything specific to this situation. Also tried layoutIfNeeded()
suggested here, but it didn't help: Animate view height with Swift
Any help much appreciated!
Upvotes: 1
Views: 2289
Reputation: 52153
You should update the constant of the constraint outside of the animation block:
self.collectionViewHeight.constant = 0
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut,
animations: view.layoutIfNeeded, completion: nil)
(view
is the superview of the collection view in question.)
Upvotes: 5