hjouhi hjouhi - 1 year ago 63
Swift Question

Animating collection view height change (Swift)

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
suggested here, but it didn't help: Animate view height with Swift

Any help much appreciated!

Answer Source

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.)