moonvader moonvader - 7 months ago 41
Swift Question

Animate view height with Swift

I have view sView inside my ViewController. It height has constraint - I created IBOutlet for this constraint - sViewHeight. I want to decrease height of sView with animation.

I created function

UIView.animateWithDuration(5.5, animations: {
self.sViewHeight.constant = 50
})


Height of view is changing but i don't see any animation. What I am doing wrong?

Answer

use layoutIfNeeded()

 self.view.layoutIfNeeded() // force any pending operations to finish

 UIView.animateWithDuration(0.2, animations: { () -> Void in
    self.sViewHeight.constant = 50
    self.view.layoutIfNeeded()
})