Instance Method

constraintEqualToAnchor:

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

Declaration

- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor;

Parameters

anchor

A layout anchor from a UIView, NSView, or UILayoutGuide object. You must use a subclass of NSLayoutAnchor that matches the current anchor. For example, if you call this method on an NSLayoutXAxisAnchor object, this parameter must be another NSLayoutXAxisAnchor.

Return Value

An 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.

// 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;

See Also

Building Constraints

- constraintEqualToAnchor:constant:

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

- constraintGreaterThanOrEqualToAnchor:

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

- constraintGreaterThanOrEqualToAnchor:constant:

Returns a constraint that defines one item’s attribute as greater than or equal to another item’s attribute plus a constant offset.

- constraintLessThanOrEqualToAnchor:

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

- constraintLessThanOrEqualToAnchor:constant:

Returns a constraint that defines one item’s attribute as less than or equal to another item’s attribute plus a constant offset.