<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/NSLayoutAnchor",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSLayoutAnchor"
  },
  "title" : "NSLayoutAnchor"
}
-->

# NSLayoutAnchor

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

```
@MainActor class NSLayoutAnchor<AnchorType> where AnchorType : AnyObject
```

## Overview

Use these constraints to programatically define your layout using Auto Layout. Instead of creating [`NSLayoutConstraint`](/documentation/UIKit/NSLayoutConstraint) objects directly, start with a [`UIView`](/documentation/UIKit/UIView), <doc://com.apple.documentation/documentation/AppKit/NSView>, or [`UILayoutGuide`](/documentation/UIKit/UILayoutGuide) object you wish to constrain, and select one of that object’s anchor properties. These properties correspond to the main [`NSLayoutConstraint.Attribute`](/documentation/UIKit/NSLayoutConstraint/Attribute) values used in Auto Layout, and provide an appropriate [`NSLayoutAnchor`](/documentation/UIKit/NSLayoutAnchor) subclass for creating constraints to that attribute. Use the anchor’s methods to construct your constraint.

> Note:
> ``doc://com.apple.uikit/documentation/UIKit/UIView`` does not provide anchor properties for the layout margin attributes. Instead, the ``doc://com.apple.uikit/documentation/UIKit/UIView/layoutMarginsGuide`` property provides a ``doc://com.apple.uikit/documentation/UIKit/UILayoutGuide`` object that represents these margins. Use the guide’s anchor properties to create your constraints.

```swift
// 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`](/documentation/UIKit/NSLayoutAnchor) class provides several advantages over using the [`NSLayoutConstraint`](/documentation/UIKit/NSLayoutConstraint) API directly.

- The code is cleaner, more concise, and easier to read.
- The ``doc://com.apple.uikit/documentation/UIKit/NSLayoutConstraint/Attribute`` subclasses provide additional type checking, preventing you from creating invalid constraints.

> Note:
> While the ``doc://com.apple.uikit/documentation/UIKit/NSLayoutAnchor`` class provides additional type checking, it is still possible to create invalid constraints. For example, the compiler allows you to constrain one view’s ``doc://com.apple.uikit/documentation/UIKit/UIView/leadingAnchor`` with another view’s ``doc://com.apple.uikit/documentation/UIKit/UIView/leftAnchor``, since they are both ``doc://com.apple.uikit/documentation/UIKit/NSLayoutXAxisAnchor`` instances. However, Auto Layout does not allow constraints that mix leading and trailing attributes with left or right attributes. As a result, this constraint crashes at runtime.

For more information on the anchor properties, see <doc://com.apple.documentation/documentation/AppKit/NSView/bottomAnchor> in the [`UIView`](/documentation/UIKit/UIView), <doc://com.apple.documentation/documentation/AppKit/NSView>, or [`UILayoutGuide`](/documentation/UIKit/UILayoutGuide).

> Note:
> You never use the ``doc://com.apple.uikit/documentation/UIKit/NSLayoutAnchor`` class directly. Instead, use one of its subclasses, based on the type of constraint you wish to create.
> 
> - Use ``doc://com.apple.uikit/documentation/UIKit/NSLayoutXAxisAnchor`` to create horizontal constraints.
> - Use ``doc://com.apple.uikit/documentation/UIKit/NSLayoutYAxisAnchor`` to create vertical constraints.
> - Use ``doc://com.apple.uikit/documentation/UIKit/NSLayoutDimension`` to create constraints that affect the view’s height or width.
> 
> However, since you access ``doc://com.apple.uikit/documentation/UIKit/NSLayoutAnchor`` objects using the anchor properties of a ``doc://com.apple.uikit/documentation/UIKit/UIView``, <doc://com.apple.documentation/documentation/AppKit/NSView>, or ``doc://com.apple.uikit/documentation/UIKit/UILayoutGuide``, a correct subclass is automatically provided.

## Topics

### Building constraints

[`constraint(equalTo:)`](/documentation/UIKit/NSLayoutAnchor/constraint(equalTo:))

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

[`constraint(equalTo:constant:)`](/documentation/UIKit/NSLayoutAnchor/constraint(equalTo:constant:))

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

[`constraint(greaterThanOrEqualTo:)`](/documentation/UIKit/NSLayoutAnchor/constraint(greaterThanOrEqualTo:))

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

[`constraint(greaterThanOrEqualTo:constant:)`](/documentation/UIKit/NSLayoutAnchor/constraint(greaterThanOrEqualTo:constant:))

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

[`constraint(lessThanOrEqualTo:)`](/documentation/UIKit/NSLayoutAnchor/constraint(lessThanOrEqualTo:))

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

[`constraint(lessThanOrEqualTo:constant:)`](/documentation/UIKit/NSLayoutAnchor/constraint(lessThanOrEqualTo: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

  <doc://com.apple.documentation/documentation/AppKit/NSLayoutAnchor/constraintsAffectingLayout>

  <doc://com.apple.documentation/documentation/AppKit/NSLayoutAnchor/hasAmbiguousLayout>

  <doc://com.apple.documentation/documentation/AppKit/NSLayoutAnchor/name>

  <doc://com.apple.documentation/documentation/AppKit/NSLayoutAnchor/item>



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)
