How to Set Priority on Constraints in Swift

How can I change constraints priority in run time

As stated in NSLayoutConstraint class reference:

Priorities may not change from nonrequired to required, or from required to nonrequired. An exception will be thrown if a priority of NSLayoutPriorityRequired in OS X or UILayoutPriorityRequired in iOS is changed to a lower priority, or if a lower priority is changed to a required priority after the constraints is added to a view. Changing from one optional priority to another optional priority is allowed even after the constraint is installed on a view.

Use priority 999 instead of 1000. It won't be absolutely required technically speaking, but it'll be a higher priority than anything else.

How can I set priority on constraints in Swift?

It actually seems to take three lines of code to set up a prioritized "anchor-based" constraint in code:

let widthConstraint = meetingFormView.widthAnchor.constraint(equalToConstant: 170)
widthConstraint.priority = UILayoutPriority(rawValue: 500)
widthConstraint.isActive = true

It seems like if I try to set isActive with the let declare Xcode (maybe Swift?) doesn't recognize that the type is NSLayoutConstraint. And using UILayoutPriority(rawValue:) seems to be the best (only?) way to set priorities.

While this answer doesn't conform exactly with what you are doing, I believe it will work with IB. Just replace the let with creating an IBOutlet and there should be no need for the isActive line.

Obviously, to change the priority later in code you just need:

widthConstraint.priority = UILayoutPriority(rawValue: 750)

I don't understand constraints priority

The priority for the constrains will be applied in order to resolve a conflict between two different constraints. The frame's view will be modified applying the constraint with more priority. So, you should have another view or use the superview to apply the priority to the constraints.

This is a nice answer explaining the resistance priority:

Cocoa Autolayout: content hugging vs content compression resistance priority

satisfying two auto layout constraints programmatically - Swift

You will need to set lower priority for the "ratio" constraint

let constraint = button.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.43)
constraint.priority = .defaultHigh
constraint.isActive = true

if the engine still have troubles with figuring out the layout, you can try different priority value.

UILayoutPriority documentation

Adding priority to layout constraints

You just set the priority property of the constraint, like so:

NSLayoutConstraint *centeringConstraint = 
[NSLayoutConstraint constraintWithItem:_label
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];

centeringConstraint.priority = 800; // <-- this line

[self addConstraint:centeringConstraint];


Related Topics



Leave a reply



Submit