Class

NSLayoutAnchor

A factory class for creating layout constraint objects using a fluent API.

Declaration

@interface NSLayoutAnchor<__covariant AnchorType> : NSObject

Overview

Use these constraints to programatically define your layout using Auto Layout. Instead of creating NSLayoutConstraint objects directly, start with a UIView, NSView, or UILayoutGuide object you wish to constrain, and select one of that object’s anchor properties. These properties correspond to the main NSLayoutAttribute values used in Auto Layout, and provide an appropriate NSLayoutAnchor subclass for creating constraints to that attribute. Use the anchor’s methods to construct your constraint.

// Creating constraints using NSLayoutConstraint
[NSLayoutConstraint
 constraintWithItem:subview
 attribute:NSLayoutAttributeLeading
 relatedBy:NSLayoutRelationEqual
 toItem:self.view
 attribute:NSLayoutAttributeLeadingMargin
 multiplier:1.0
 constant:0.0].active = YES;
 
[NSLayoutConstraint
 constraintWithItem:subview
 attribute:NSLayoutAttributeTrailing
 relatedBy:NSLayoutRelationEqual
 toItem:self.view
 attribute:NSLayoutAttributeTrailingMargin
 multiplier:1.0
 constant:0.0].active = YES;
 
// Creating the same constraints using Layout Anchors
UILayoutGuide *margin = self.view.layoutMarginsGuide;
 
[subview.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
[subview.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;

As you can see from these examples, the NSLayoutAnchor class provides several advantages over using the NSLayoutConstraint API directly.

  • The code is cleaner, more concise, and easier to read.

  • The NSLayoutAttribute subclasses provide additional type checking, preventing you from creating invalid constraints.

For more information on the anchor properties, see bottomAnchor in the UIView, NSView, or UILayoutGuide.

Topics

Building Constraints

- constraintEqualToAnchor:

Returns a constraint that defines one item’s attribute as equal to another.

- constraintEqualToAnchor:constant:

Returns a constraint that defines one item’s attribute as equal to another item’s attribute plus a constant offset.

- constraintGreaterThanOrEqualToAnchor:

Returns a constraint that defines one item’s attribute as greater than or equal to another.

- constraintGreaterThanOrEqualToAnchor:constant:

Returns a constraint that defines one item’s attribute as greater than or equal to another item’s attribute plus a constant offset.

- constraintLessThanOrEqualToAnchor:

Returns a constraint that defines one item’s attribute as less than or equal to another.

- constraintLessThanOrEqualToAnchor:constant:

Returns a constraint that defines one item’s attribute as less than or equal to another item’s attribute plus a constant offset.

Debugging the Anchor

constraintsAffectingLayout

The constraints that impact the layout of the anchor.

hasAmbiguousLayout

A Boolean value indicating whether the constraints impacting the anchor specify its location ambiguously.

name

The name assigned to the anchor for debugging purposes.

item

The layout item used to calculate the anchor's position.

Relationships

Inherits From

Conforms To

See Also

Anchors

NSLayoutXAxisAnchor

A factory class for creating horizontal layout constraint objects using a fluent API.

NSLayoutYAxisAnchor

A factory class for creating vertical layout constraint objects using a fluent API.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software