How to change bottom layout constraint in iOS, Swift How to change bottom layout constraint in iOS, Swift xcode xcode

How to change bottom layout constraint in iOS, Swift


Take the constraint as IBOutlet of NSLayoutConstraint.

enter image description here

Set the constraint outlets and change constant value by :

self.sampleConstraint.constant = 20self.view.layoutIfNeeded()


If you are adding constraint programatically like this:

var constraintButton = NSLayoutConstraint (item: buttonPlay,                                            attribute: NSLayoutAttribute.Bottom,                                            relatedBy: NSLayoutRelation.Equal,                                            toItem: self.view,                                            attribute: NSLayoutAttribute.Bottom,                                            multiplier: 1,                                           constant: 0)// Add the constraint to the viewself.view.addConstraint(constraintButton)

Then you can update it this way:

self.constraintButton.constant = 50self.view.layoutIfNeeded()

And if you want that with animation you can do it this way:

self.view.layoutIfNeeded()UIView.animateWithDuration(1, animations: {    self.constraintButton.constant = 50    self.view.layoutIfNeeded()})

Hope it helps.


Create an IBOutlet for your constraint:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomContraint;

And when you need to change it call:

bottomContstraint.constant = //your valueview.layoutIfNeeded()

Also you can animate constraint change like that:

bottomContstraint.constant = //your valueUIView.animateWithDuration(0.5, animations: {  self.view.layoutIfNeeded()})