NSLayoutConstraint: constant property
The constant property is the constant term of the constraint's equation (firstAnchor = secondAnchor × multiplier + constant). It's the only number in a constraint you can change after creating it.
Value
A number, in pt.
- Readable and writable.
- Defaults to the
constantpassed when the constraint was created, or0if none was passed. For a constraint to a constant (theconstraintEqualToConstant()family), it's that constant. - Positive is to the right horizontally and downward vertically.
- A change takes effect on the next layout pass. To read the new
frameright away, calllayoutIfNeeded()first. The change is instant, with no transition animation; to animate it, see the guide Animating views. - Accepts values that convert to a number;
'84'and84have the same effect. - There's no change notification. Only the app's own code changes it.
Invalid values
Writing a value that doesn't convert to a finite number (undefined, 'abc', NaN) throws TypeError: constant must be a finite number, got undefined, ending with the value received. constant keeps its previous value.
Examples
js
const parent = new UIView();
const box = new UIView();
box.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(box);
const height = box.heightAnchor.constraintEqualToConstant(60);
height.isActive = true;
parent.layoutIfNeeded();
height.constant = 84;
parent.layoutIfNeeded();
console.log(box.frame.height);Result: Prints 84.
Corresponding iOS API
NSLayoutConstraint.constant.
See also
- Expanding and collapsing
multiplier: can't be changed after creation