22

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

I created function

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

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

Elijah
  • 8,381
  • 2
  • 55
  • 49
moonvader
  • 19,761
  • 18
  • 67
  • 116

2 Answers2

78

use layoutIfNeeded()

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

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

swift 3

view.layoutIfNeeded() 
sViewHeightConstraint.constant = 50

UIView.animate(withDuration: 1.0, animations: { 
     self.view.layoutIfNeeded() 
})
Elijah
  • 8,381
  • 2
  • 55
  • 49
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
0

In case your view updates but animation isn't working, this solution works for me.

UIView.animate(withDuration: 0.2, animations: { 
    self.sViewHeightConstraint.constant = 50
    self.sView.size = CGSize(width: self.sView.frame.width, height: 50)
    self.view.layoutIfNeeded() 
})