NSLayoutAnchor
A factory class for creating layout constraint objects using a fluent API.
Declaration
class NSLayoutAnchor<AnchorType> where AnchorType : AnyObjectOverview
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.
For more information on the anchor properties, see bottomAnchor in the NSView or NSLayoutGuide.
Topics
Building constraints
constraint(equalTo:)constraint(equalTo:constant:)constraint(greaterThanOrEqualTo:)constraint(greaterThanOrEqualTo:constant:)constraint(lessThanOrEqualTo:)constraint(lessThanOrEqualTo:constant:)