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

# NSLayoutAnchor

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

## Declaration

```swift
@MainActor 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 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 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. note: UIView does not provide anchor properties for the layout margin attributes. Instead, the layoutMarginsGuide property provides a UILayoutGuide object that represents these margins. Use the guide’s anchor properties to create your constraints. 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 UIView, NSView, or UILayoutGuide. 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 a UIView, NSView, or UILayoutGuide, a correct subclass is automatically provided.

## Topics

### Building constraints

- [constraint(equalTo:)](uikit/nslayoutanchor/constraint(equalto:).md)
- [constraint(equalTo:constant:)](uikit/nslayoutanchor/constraint(equalto:constant:).md)
- [constraint(greaterThanOrEqualTo:)](uikit/nslayoutanchor/constraint(greaterthanorequalto:).md)
- [constraint(greaterThanOrEqualTo:constant:)](uikit/nslayoutanchor/constraint(greaterthanorequalto:constant:).md)
- [constraint(lessThanOrEqualTo:)](uikit/nslayoutanchor/constraint(lessthanorequalto:).md)
- [constraint(lessThanOrEqualTo:constant:)](uikit/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:)](uikit/nslayoutanchor/init(coder:).md)

## Relationships

### Inherits From

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

### Inherited By

- [NSLayoutDimension](uikit/nslayoutdimension.md)
- [NSLayoutXAxisAnchor](uikit/nslayoutxaxisanchor.md)
- [NSLayoutYAxisAnchor](uikit/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)
- [Sendable](swift/sendable.md)

## See Also

### Anchors

- [NSLayoutXAxisAnchor](uikit/nslayoutxaxisanchor.md)
- [NSLayoutYAxisAnchor](uikit/nslayoutyaxisanchor.md)
- [NSLAYOUTANCHOR_H](uikit/nslayoutanchor_h.md)
