I want to animate an object, so I declare a constraint and add it to the view. I then update the constant
property of the constraint inside an UIView
animation. Why doesn't this code move the object?
UIView.animateWithDuration(1, animations: {
myConstraint.constant = 0
self.view.updateConstraints(myConstraint)
})
Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code. Method updateConstraints() is an instance method of UIView . It is helpful when you are setting the constraints programmatically.
Clicking the Edit button in any of the constraints brings up a popover where you can change the constraint's relationship, constant, priority, or multiplier. To make additional changes, double-click the constraint to select it and open it in the Attribute inspector.
The relationship between two user interface objects that must be satisfied by the constraint-based layout system.
In order to declare an animation, you cannot re-define the constraint and call updateConstraints
. You are supposed to change the constant
of your constraint and follow the format below:
self.view.layoutIfNeeded() UIView.animate(withDuration: 1) { self.sampleConstraint.constant = 20 self.view.layoutIfNeeded() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With