Generic Class

NSLayoutAnchor

The NSLayoutAnchor class is a factory class for creating NSLayoutConstraint objects using a fluent API. Use these constraints to programatically define your layout using Auto Layout.

Overview

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(item: subview,
                   attribute: .leading,
                   relatedBy: .equal,
                   toItem: view,
                   attribute: .leadingMargin,
                   multiplier: 1.0,
                   constant: 0.0).isActive = true

NSLayoutConstraint(item: subview,
                   attribute: .trailing,
                   relatedBy: .equal,
                   toItem: view,
                   attribute: .trailingMargin,
                   multiplier: 1.0,
                   constant: 0.0).isActive = true


// Creating the same constraints using Layout Anchors
let margins = view.layoutMarginsGuide

subview.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
subview.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true

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.

Symbols

Building Constraints

func constraint(equalTo: NSLayoutAnchor<AnchorType>)

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

func constraint(equalTo: NSLayoutAnchor<AnchorType>, constant: CGFloat)

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

func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>)

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

func constraint(greaterThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat)

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

func constraint(lessThanOrEqualTo: NSLayoutAnchor<AnchorType>)

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

func constraint(lessThanOrEqualTo: NSLayoutAnchor<AnchorType>, constant: CGFloat)

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

Relationships

Generic Constraints

  • AnchorType : AnyObject

Inherits From