1. 程式人生 > >通過程式碼 新增 和 修改 NSLayoutConstraint

通過程式碼 新增 和 修改 NSLayoutConstraint

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 =50self.view.layoutIfNeeded()})