NSLayoutAnchor: constraintLessThanOrEqualToAnchor() method
The constraintLessThanOrEqualToAnchor() method creates a constraint: this anchor is less than or 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.constraintLessThanOrEqualToAnchor().
Syntax
constraintLessThanOrEqualToAnchor(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):constraintLessThanOrEqualToAnchor: needs an anchor as its argument. It begins with the full native method name; whenconstantis given it isconstraintLessThanOrEqualToAnchor: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.topAnchor.constraintLessThanOrEqualToAnchor(a.bottomAnchor, { constant: 20 });
c.isActive = true;
console.log(c.relation, c.constant);Result: b's top edge is at most 20 pt below a's bottom edge. The console logs lessThanOrEqual 20.
Corresponding iOS API
-[NSLayoutAnchor constraintLessThanOrEqualToAnchor:] and -[NSLayoutAnchor constraintLessThanOrEqualToAnchor:constant:] are the same method in JS, with constant: written as an options key.