---
title: NSLayoutAnchor
framework: appkit
role: symbol
role_heading: Class
path: appkit/nslayoutanchor
---

# NSLayoutAnchor

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

## Declaration

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

## Overview

Overview Use these constraints to programatically define your layout using Auto Layout. Instead of creating NSLayoutConstraint objects directly, start with an NSView or NSLayoutGuide object you wish to constrain, and select one of that object’s anchor properties. These properties correspond to the main NSLayoutConstraint.Attribute 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. 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 NSLayoutConstraint.Attribute subclasses provide additional type checking, preventing you from creating invalid constraints. note: While the 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 leadingAnchor with another view’s leftAnchor, since they are both 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 in the NSView or NSLayoutGuide. note: You never use the NSLayoutAnchor class directly. Instead, use one of its subclasses, based on the type of constraint you wish to create. Use NSLayoutXAxisAnchor to create horizontal constraints. Use NSLayoutYAxisAnchor to create vertical constraints. Use NSLayoutDimension to create constraints that affect the view’s height or width. However, since you access NSLayoutAnchor objects using the anchor properties of an NSView or NSLayoutGuide, a correct subclass is automatically provided.

## Topics

### Building constraints

- [constraint(equalTo:)](appkit/nslayoutanchor/constraint(equalto:).md)
- [constraint(equalTo:constant:)](appkit/nslayoutanchor/constraint(equalto:constant:).md)
- [constraint(greaterThanOrEqualTo:)](appkit/nslayoutanchor/constraint(greaterthanorequalto:).md)
- [constraint(greaterThanOrEqualTo:constant:)](appkit/nslayoutanchor/constraint(greaterthanorequalto:constant:).md)
- [constraint(lessThanOrEqualTo:)](appkit/nslayoutanchor/constraint(lessthanorequalto:).md)
- [constraint(lessThanOrEqualTo:constant:)](appkit/nslayoutanchor/constraint(lessthanorequalto:constant:).md)

### Debugging the anchor

- [constraintsAffectingLayout](appkit/nslayoutanchor/constraintsaffectinglayout.md)
- [hasAmbiguousLayout](appkit/nslayoutanchor/hasambiguouslayout.md)
- [name](appkit/nslayoutanchor/name.md)
- [item](appkit/nslayoutanchor/item.md)

### Initializers

- [init(coder:)](appkit/nslayoutanchor/init(coder:).md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [NSLayoutDimension](appkit/nslayoutdimension.md)
- [NSLayoutXAxisAnchor](appkit/nslayoutxaxisanchor.md)
- [NSLayoutYAxisAnchor](appkit/nslayoutyaxisanchor.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Anchors

- [NSLayoutXAxisAnchor](appkit/nslayoutxaxisanchor.md)
- [NSLayoutYAxisAnchor](appkit/nslayoutyaxisanchor.md)
