The relationship between two user interface objects that must be satisfied by the constraint-based layout system.


@interface NSLayoutConstraint : NSObject


Each constraint is a linear equation with the following format:

item1.attribute1 = multiplier × item2.attribute2 + constant

In this equation, attribute1 and attribute2 are the variables that Auto Layout can adjust when solving these constraints. The other values are defined when you create the constraint. For example, If you’re defining the relative position of two buttons, you might say “the leading edge of the second button should be 8 points after the trailing edge of the first button.” The linear equation for this relationship is shown below:

// positive values move to the right in left-to-right languages like English.
button2.leading = 1.0 × button1.trailing + 8.0

Auto Layout then modifies the values of the specified leading and trailing edges until both sides of the equation are equal. Note that Auto Layout does not simply assign the value of the right side of this equation to the left side. Instead, the system can modify either attribute or both attributes as needed to solve for this constraint.

The fact that constraints are equations (and not assignment operators) means that you can switch the order of the items in the equation as needed to more clearly express the desired relationship. However, if you switch the order, you must also invert the multiplier and constant. For example, the following two equations produce identical constraints:

// These equations produce identical constraints
button2.leading = 1.0 × button1.trailing + 8.0
button1.trailing = 1.0 × button2.leading - 8.0

A valid layout is defined as a set constraints with one and only one possible solution. Valid layouts are also referred to as a nonambiguous, nonconflicting layouts. Constraints with more than one solution are ambiguous. Constraints with no valid solutions are conflicting. For more information on resolving ambiguous and conflicting constraints, see Types of Errors in Auto Layout Guide.

Additionally, constraints are not limited to equality relationships. They can also use greater than or equal to (>=) or less than or equal to (<=) to describe the relationship between the two attributes. Constraints also have priorities between 1 and 1,000. Constraints with a priority of 1,000 are required. All priorities less than 1,000 are optional. By default, all constraints are required (priority = 1,000).

After solving for the required constraints, Auto Layout tries to solve all the optional constraints in priority order from highest to lowest. If it cannot solve for an optional constraint, it tries to come as close as possible to the desired result, and then moves on to the next constraint.

This combination of inequalities, equalities, and priorities gives you a great amount of flexibility and power. By combining multiple constraints, you can define layouts that dynamically adapt as the size and location of the elements in your user interface change. For some example layouts, see Stack Views in Auto Layout Guide.


Creating Constraints

+ constraintsWithVisualFormat:options:metrics:views:

Creates constraints described by an ASCII art-like visual format string.

+ constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:

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

Activating and Deactivating Constraints


The active state of the constraint.

+ activateConstraints:

Activates each constraint in the specified array.

+ deactivateConstraints:

Deactivates each constraint in the specified array.

Accessing Constraint Data


The first object participating in the constraint.


The attribute of the first object participating in the constraint.


The relation between the two attributes in the constraint.


The second object participating in the constraint.


The attribute of the second object participating in the constraint.


The multiplier applied to the second attribute participating in the constraint.


The constant added to the multiplied second attribute participating in the constraint.


The first anchor that defines the constraint.


The second anchor that defines the constraint.

Getting the Layout Priority


The priority of the constraint.


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.


A required constraint.


The priority level with which a button resists compressing its content.


The priority level at which a button hugs its contents horizontally.


The priority level with which the view wants to conform to the target size in that computation.


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.

Identifying a Constraint


The name that identifies the constraint.

Controlling Constraint Archiving


A Boolean value that determines whether the constraint should be archived by its owning view.



The relation between the first attribute and the modified second attribute in a constraint.


The part of the object’s visual representation that should be used to get the value for the constraint.


A bit mask that specifies both a part of an interface element to align and a direction for the alignment between two interface elements.


The layout constraint orientation, either horizontal or vertical, that the constraint uses to enforce layout between objects.


A description of the distance between the edges of two rectangles.


Inherits From

See Also


Positioning Content Within Layout Margins

Position views so that they are not crowded by other content.

Positioning Content Relative to the Safe Area

Position views so that they are not obstructed by other content.


A set of methods that provide layout support and access to layout anchors.


Creates a dictionary wherein the keys are string representations of the corresponding values’ variable names.