<!--
{
  "availability" : [
    "macOS: 10.11.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSLayoutAnchor",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSLayoutAnchor"
  },
  "title" : "NSLayoutAnchor"
}
-->

# NSLayoutAnchor

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

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

## Overview

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

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

- The code is cleaner, more concise, and easier to read.
- The [`NSLayoutConstraint.Attribute`](/documentation/AppKit/NSLayoutConstraint/Attribute) subclasses provide additional type checking, preventing you from creating invalid constraints.

> Note:
> While the ``doc://com.apple.appkit/documentation/AppKit/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.appkit/documentation/AppKit/NSView/leadingAnchor`` with another view’s ``doc://com.apple.appkit/documentation/AppKit/NSView/leftAnchor``, since they are both ``doc://com.apple.appkit/documentation/AppKit/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 [`bottomAnchor`](/documentation/AppKit/NSView/bottomAnchor) in the [`NSView`](/documentation/AppKit/NSView) or [`NSLayoutGuide`](/documentation/AppKit/NSLayoutGuide).

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

## Topics

### Building constraints

[`-  constraintEqualToAnchor:`](/documentation/AppKit/NSLayoutAnchor/constraint(equalTo:))

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

[`-  constraintEqualToAnchor:constant:`](/documentation/AppKit/NSLayoutAnchor/constraint(equalTo:constant:))

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

[`-  constraintGreaterThanOrEqualToAnchor:`](/documentation/AppKit/NSLayoutAnchor/constraint(greaterThanOrEqualTo:))

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

[`-  constraintGreaterThanOrEqualToAnchor:constant:`](/documentation/AppKit/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.

[`-  constraintLessThanOrEqualToAnchor:`](/documentation/AppKit/NSLayoutAnchor/constraint(lessThanOrEqualTo:))

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

[`-  constraintLessThanOrEqualToAnchor:constant:`](/documentation/AppKit/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

[`constraintsAffectingLayout`](/documentation/AppKit/NSLayoutAnchor/constraintsAffectingLayout)

The constraints that impact the layout of the anchor.

[`hasAmbiguousLayout`](/documentation/AppKit/NSLayoutAnchor/hasAmbiguousLayout)

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

[`name`](/documentation/AppKit/NSLayoutAnchor/name)

The name assigned to the anchor for debugging purposes.

[`item`](/documentation/AppKit/NSLayoutAnchor/item)

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

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Inherited By

[`NSLayoutYAxisAnchor`](/documentation/AppKit/NSLayoutYAxisAnchor)

[`NSLayoutXAxisAnchor`](/documentation/AppKit/NSLayoutXAxisAnchor)

[`NSLayoutDimension`](/documentation/AppKit/NSLayoutDimension)

---

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)