UIView: centerXAnchor property
The centerXAnchor property is the view's horizontal center line, used to build constraints.
Value
- Read-only; assigning to it throws
TypeError. - Reading it repeatedly on the same view returns the same object:
view.centerXAnchor === view.centerXAnchoristrue. - Anchors can't be constructed yourself. If your code holds only the anchor and not the view, the anchor still works.
- Constraints can only be built with an anchor of the same type. See
NSLayoutAnchor.
Examples
js
const parent = new UIView();
const box = new UIView();
box.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(box);
box.centerXAnchor.constraintEqualToAnchor(parent.centerXAnchor).isActive = true;Effect: box is centered horizontally in parent.
Corresponding iOS API
UIView.centerXAnchor.
See also
NSLayoutXAxisAnchor: which constraints it can build- Laying out with constraints