Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate view height with Swift

Tags:

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?

like image 443
moonvader Avatar asked Apr 30 '16 10:04

moonvader


People also ask

How do I change the height of a view in Swift?

1) Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select "Equal heights". 2)Then go to size inspector of the frame, click edit "Equal height to Superview" constraint, set the multiplier to 0.7 and hit return.

How do you animate a view in Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.


1 Answers

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()  }) 
like image 71
EI Captain v2.0 Avatar answered Oct 02 '22 09:10

EI Captain v2.0