change constraints in code

If I've already set contraints using code in iOS, how do I change the constraits?

Answered by Claude31 in 261483022

If you create a constraint :

var firstConstraint = NSLayoutConstraint(item: labelName, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0.0)

then you can activate:

firstConstraint.isActive = true

and change its properties:

firstConstraint.constant = 10
firstConstraint.multiplier = 1.2


Then you have to redraw the views with setNeedsDisplay.

Do you mean you have set constraints in IB and now want to change in code ?


- Create an IBOutlet for the constraint (control drag from selected constraint in object navigator)

         @IBOutlet weak private var okLeadingConstraint  : NSLayoutConstraint!

- then set the parameters as needed ; for instance

                okLeadingConstraint.constant = 10

or

                okLeadingConstraint.active = false  // If you want to deactivate

You can also create constraint in code, with NSLayoutConstraint

NSLayoutConstraint(item: Any, attribute: NSLayoutAttribute, relatedBy: NSLayoutRelation, toItem: Any?, attribute: NSLayoutAttribute, multiplier: CGFloat, constant: CGFloat)

This creates a constraint that defines the relationship between the specified attributes of the given views.

I apologize my original question wasn't clear.


I mean if I created a constraint in code, such as:


NSLayoutConstraint(item: labelName, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0.0).isActive = true


Can I later change it with another code like:


NSLayoutConstraint(item: labelName, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 5.0).isActive = true


Where I repeat the previous statement above with a different value for the parameter constant:. Would that add another constraint, or would it replace the one I already set in the first statement.


I tried using a variable to hold a reference to the constraint such as so:


let layoutConstraint = NSLayoutConstraint(item: labelName, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0.0).isActive = true


Then I changed the value of the constant to 5.0 like this:


layoutConstraint.constant = 5.0


I don't know what to do after that. Is there a statement I need to execute after that to update the constraint to the new value(s)?

Accepted Answer

If you create a constraint :

var firstConstraint = NSLayoutConstraint(item: labelName, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1.0, constant: 0.0)

then you can activate:

firstConstraint.isActive = true

and change its properties:

firstConstraint.constant = 10
firstConstraint.multiplier = 1.2


Then you have to redraw the views with setNeedsDisplay.

Ok. Thanks.

If anyone's looking for a concrete tutorial on how to do this, I wrote one that has source code: https://www.delasign.com/blog/swift-modify-constraints/

change constraints in code
 
 
Q