Animate view height with Swift

28,585

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() 
})
Share:
28,585

Related videos on Youtube

moonvader
Author by

moonvader

web, dev, Wordpress, Arduino, iOS, Flutter, Mobile

Updated on July 10, 2022

Comments

  • moonvader
    moonvader almost 2 years

    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?

  • Bhavin Bhadani
    Bhavin Bhadani almost 8 years
    @moonvader layoutIfNeeded on the view force any pending operations to finish and then in the animation block capture the frame changes.
  • Bhavin Bhadani
    Bhavin Bhadani almost 8 years