NSLayoutAnchor: constraintEqualToAnchor() method
The constraintEqualToAnchor() method creates a constraint: this anchor is equal to another anchor plus a constant (this anchor = other + constant).
NSLayoutXAxisAnchor, NSLayoutYAxisAnchor, and NSLayoutDimension all have this method. The NSLayoutDimension version also accepts multiplier; see NSLayoutDimension.constraintEqualToAnchor().
Syntax
constraintEqualToAnchor(other[, { constant }])Parameters
other
Another anchor of the same type: horizontal positions connect only to horizontal positions, vertical positions only to vertical positions, and sizes only to sizes.
options.constant
A number, in pt. Optional; defaults to 0. Positive is to the right horizontally and down vertically. Writing { constant: undefined } is the same as omitting it. You can change the constraint's constant later.
Return value
A new NSLayoutConstraint. It is not yet active; it takes part in layout only after being activated with NSLayoutConstraint.activateConstraints() or isActive.
Exceptions
TypeError
otheris an anchor of another type:NSLayoutYAxisAnchor cannot be constrained to NSLayoutXAxisAnchor: their units are not compatible, with the actual class names.otheris not an anchor (includingnull):constraintEqualToAnchor: needs an anchor as its argument. It begins with the full native method name; whenconstantis given it isconstraintEqualToAnchor:constant:.constantcannot be converted to a finite number:constant must be a finite number, got "abc".
Type checking reports anchors of the wrong type in the editor.
Examples
const parent = new UIView();
const a = new UIView();
const b = new UIView();
for (const v of [a, b]) {
v.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(v);
}
const c = b.leadingAnchor.constraintEqualToAnchor(a.trailingAnchor, { constant: 8 });
c.isActive = true;
console.log(c.relation, c.constant);Result: b's left edge is 8 pt to the right of a's right edge. The console logs equal 8.
Corresponding iOS API
-[NSLayoutAnchor constraintEqualToAnchor:] and -[NSLayoutAnchor constraintEqualToAnchor:constant:] are the same method in JS, with constant: written as an options key.