<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/NSLayoutAnchor/constraint(equalTo:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "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 a [`UIView`](/documentation/UIKit/UIView), <doc://com.apple.documentation/documentation/AppKit/NSView>, or [`UILayoutGuide`](/documentation/UIKit/UILayoutGuide) object. You must use a subclass of [`NSLayoutAnchor`](/documentation/UIKit/NSLayoutAnchor) that matches the current anchor. For example, if you call this method on an [`NSLayoutXAxisAnchor`](/documentation/UIKit/NSLayoutXAxisAnchor) object, this parameter must be another [`NSLayoutXAxisAnchor`](/documentation/UIKit/NSLayoutXAxisAnchor).

## Return Value

An [`NSLayoutConstraint`](/documentation/UIKit/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.

```objc
// Creating a constraint using NSLayoutConstraint
[NSLayoutConstraint
 constraintWithItem:subview
 attribute:NSLayoutAttributeLeading
 relatedBy:NSLayoutRelationEqual
 toItem:self.view
 attribute:NSLayoutAttributeLeadingMargin
 multiplier:1.0
 constant:0.0].active = YES;
 
// Creating the same constraint using constraintEqualToAnchor:
UILayoutGuide *margin = self.view.layoutMarginsGuide;
[subview.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
```

---

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)