1

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!

Community
  • 1
  • 1
hjouh1
  • 35
  • 1
  • 8

1 Answers1

5

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

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119