Skip to content

NSLayoutDimension: constraintEqualToAnchor() method ​

The constraintEqualToAnchor() method creates a constraint: this dimension equals another dimension times a multiplier plus a constant (this dimension = other × multiplier + constant).

It overrides the method of the same name on NSLayoutAnchor, adding multiplier.

Syntax ​

constraintEqualToAnchor(other[, { multiplier, constant }])

Parameters ​

other

Another NSLayoutDimension: a width or a height. Width and height are the same type and can constrain each other; a view's height can be constrained to its own width.

options.multiplier

A number, a ratio, unitless. Optional; defaults to 1.

options.constant

A number, in pt. Optional; defaults to 0.

Both can be given together: { multiplier: 0.5, constant: -8 } means "half, minus 8 pt".

Return value ​

A new NSLayoutConstraint. It isn't active yet; it takes part in layout only after it's activated with NSLayoutConstraint.activateConstraints() or isActive.

Exceptions ​

TypeError

  • other isn't a dimension anchor: same as NSLayoutAnchor.constraintEqualToAnchor().
  • multiplier or constant can't be converted to a finite number: multiplier must be a finite number, got "abc" (or constant …).

Examples ​

js
const parent = new UIView();
const cover = new UIView();
cover.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(cover);

const ratio = cover.heightAnchor.constraintEqualToAnchor(cover.widthAnchor, { multiplier: 9 / 16 });
ratio.isActive = true;
console.log(ratio.multiplier);

Effect: cover's height is equal to 9/16 of its width. The console prints 0.5625. The on-screen effect of multiplier is not verified in the simulator.

Corresponding iOS API ​

-[NSLayoutDimension constraintEqualToAnchor:multiplier:], -[NSLayoutDimension constraintEqualToAnchor:multiplier:constant:], and -[NSLayoutAnchor constraintEqualToAnchor:constant:] are the same method in JS; multiplier: and constant: become keys of the options object.

See also ​