Skip to content

NSLayoutXAxisAnchor: anchorWithOffsetToAnchor() method ​

The anchorWithOffsetToAnchor() method returns a dimension anchor that represents the distance from this anchor to other.

The returned anchor can form constraints just like widthAnchor. Use it to make two gaps equal, or to make a gap equal to some size.

Syntax ​

anchorWithOffsetToAnchor(other)

Parameters ​

other

Another NSLayoutXAxisAnchor.

Return value ​

An NSLayoutDimension that represents the distance between the two anchors.

Exceptions ​

TypeError

other is a different kind of anchor, or not an anchor. The error message is the same as for constraintEqualToAnchor(). This case is not verified in the simulator.

Examples ​

js
const parent = new UIView();
const left = new UIView();
const middle = new UIView();
const right = new UIView();
for (const v of [left, middle, right]) {
  v.translatesAutoresizingMaskIntoConstraints = false;
  parent.addSubview(v);
}

const gapA = left.trailingAnchor.anchorWithOffsetToAnchor(middle.leadingAnchor);
const gapB = middle.trailingAnchor.anchorWithOffsetToAnchor(right.leadingAnchor);
gapA.constraintEqualToAnchor(gapB).isActive = true;

Effect: The gap from left to middle equals the gap from middle to right. Not verified in the simulator.

Corresponding iOS API ​

-[NSLayoutXAxisAnchor anchorWithOffsetToAnchor:].

See also ​