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

# constraint(equalTo:)

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

```
func constraint(equalTo anchor: NSLayoutAnchor<AnchorType>) -> NSLayoutConstraint
```

## Parameters

`anchor`

A layout anchor from an [`NSView`](/documentation/AppKit/NSView) or [`NSLayoutGuide`](/documentation/AppKit/NSLayoutGuide) object. You must use a subclass of [`NSLayoutAnchor`](/documentation/AppKit/NSLayoutAnchor) that matches the current anchor. For example, if you call this method on an [`NSLayoutXAxisAnchor`](/documentation/AppKit/NSLayoutXAxisAnchor) object, this parameter must be another [`NSLayoutXAxisAnchor`](/documentation/AppKit/NSLayoutXAxisAnchor).

## Return Value

An [`NSLayoutConstraint`](/documentation/AppKit/NSLayoutConstraint) object that defines an equal relationship between the attributes represented by the two layout anchors.

## Discussion

This method defines the relationship `first attribute = second attribute`. Where `first attribute` is the layout attribute represented by the anchor receiving this method call, and `second attribute` is the layout attribute represented by the `anchor` parameter.

The constraints produced by the following two examples are identical.

```swift
// Creating a constraint using NSLayoutConstraint
NSLayoutConstraint(item: subview,
                   attribute: .Leading,
                   relatedBy: .Equal,
                   toItem: view,
                   attribute: .LeadingMargin,
                   multiplier: 1.0,
                   constant: 0.0).isActive = true
 
// Creating the same constraint using constraintEqualToAnchor:
let margins = view.layoutMarginsGuide
subview.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).isActive = true
```

## See Also

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

The relationship between two user interface objects that must be satisfied by the constraint-based layout system.

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

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



---

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)