UIView: leftAnchor property
The leftAnchor property is the view's left edge, used to build constraints.
Value
- Read-only; assigning to it throws
TypeError. - Reading it repeatedly on the same view returns the same object:
view.leftAnchor === view.leftAnchoristrue. - Anchors can't be constructed directly. An anchor keeps working even if your code holds only the anchor and not the view.
- An anchor can only build constraints with anchors of the same type. See
NSLayoutAnchor. - Always the left edge of the screen, regardless of writing direction. Use it only when the content really doesn't depend on writing direction. Don't mix it with
leading/trailingin the same constraint.
Examples
js
const parent = new UIView();
const box = new UIView();
box.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(box);
box.leftAnchor.constraintEqualToAnchor(parent.leftAnchor, { constant: 16 }).isActive = true;Effect: The left edge of box is 16 pt from the left edge of parent, regardless of writing direction.
Corresponding iOS API
UIView.leftAnchor.
See also
NSLayoutXAxisAnchor: which constraints it can build- Laying out with constraints