Instance Property

priority

The priority of the constraint.

Declaration

iOS, tvOS
var priority: UILayoutPriority { get set }
macOS
var priority: NSLayoutConstraint.Priority { get set }

Discussion

By default, all constraints are required; this property is set to required in macOS or UILayoutPriorityRequired in iOS.

If a constraint's priority level is less than required in macOS or UILayoutPriorityRequired in iOS, then it is optional. Higher priority constraints are satisfied before lower priority constraints; however, optional constraint satisfaction is not all or nothing. If a constraint a == b is optional, the constraint-based layout system will attempt to minimize abs(a-b).

Priorities may not change from nonrequired to required, or from required to nonrequired. An exception will be thrown if a priority of required in macOS 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.

Priorities must be greater than 0 and less than or equal to required in macOS or UILayoutPriorityRequired in iOS.

See Also

Getting the Layout Priority

struct UILayoutPriority

The layout priority is used to indicate to the constraint-based layout system which constraints are more important, allowing the system to make appropriate tradeoffs when satisfying the constraints of the system as a whole.

static let required: UILayoutPriority

A required constraint. Do not specify a layout constraint that exceeds this number.

static let defaultHigh: UILayoutPriority

This is the priority level with which a button resists compressing its content.

static let defaultLow: UILayoutPriority

This is the priority level at which a button hugs its contents horizontally.

static let fittingSizeLevel: UILayoutPriority

When you send a systemLayoutSizeFitting(_:) message to a view, the size fitting most closely to the target size is computed. This is the priority level with which the view wants to conform to the target size in that computation. It's quite low. It is generally not appropriate to make a constraint at exactly this priority. You want to be higher or lower.

struct NSLayoutConstraint.Priority

Layout priority used to indicate the relative importance of constraints, allowing Auto Layout to make appropriate tradeoffs when satisfying the constraints of the system as a whole.