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
  • 7,883
  • 2
  • 53
  • 49
moonvader
  • 16,681
  • 16
  • 62
  • 106

2 Answers2

75

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
  • 7,883
  • 2
  • 53
  • 49
Bhavin Bhadani
  • 21,946
  • 10
  • 78
  • 104
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() 
})
NeihPaine330
  • 101
  • 2