NSLayoutDimension
NSLayoutDimension represents a dimension anchor: the widthAnchor and heightAnchor of a view or layout guide, and the distance between two anchors returned by anchorWithOffsetToAnchor().
Dimension anchors can do two things position anchors can't: compare against a constant ("width = 120 pt"), and take a multiplier ("width = parent's width × 0.5"). Width and height are the same type and can constrain each other.
Inheritance: EventTarget → NSObject → NSLayoutAnchor → NSLayoutDimension
Constructor
It can't be created with new. Read it from widthAnchor or heightAnchor of a view or UILayoutGuide.
Instance methods
Comparing against a constant:
| Method | Resulting constraint |
|---|---|
constraintEqualToConstant() | This dimension = value |
constraintGreaterThanOrEqualToConstant() | This dimension ≥ value |
constraintLessThanOrEqualToConstant() | This dimension ≤ value |
Comparing against another dimension, optionally with a multiplier. These three methods override the methods of the same names on NSLayoutAnchor:
| Method | Resulting constraint |
|---|---|
constraintEqualToAnchor() | This dimension = other × multiplier + constant |
constraintGreaterThanOrEqualToAnchor() | This dimension ≥ other × multiplier + constant |
constraintLessThanOrEqualToAnchor() | This dimension ≤ other × multiplier + constant |
Examples
const parent = new UIView();
const cover = new UIView();
cover.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(cover);
NSLayoutConstraint.activateConstraints([
cover.widthAnchor.constraintEqualToAnchor(parent.widthAnchor, { multiplier: 0.5 }),
cover.heightAnchor.constraintEqualToAnchor(cover.widthAnchor, { multiplier: 9 / 16 }),
]);Effect: cover is half as wide as parent, and its height is 9/16 of its width. Not verified in the simulator.
Corresponding iOS API
| ObjC | JS |
|---|---|
[v.widthAnchor constraintEqualToConstant:60] | v.widthAnchor.constraintEqualToConstant(60) |
[a.widthAnchor constraintEqualToAnchor:b.widthAnchor multiplier:0.5] | a.widthAnchor.constraintEqualToAnchor(b.widthAnchor, { multiplier: 0.5 }) |
[a.widthAnchor constraintEqualToAnchor:b.widthAnchor multiplier:0.5 constant:-8] | a.widthAnchor.constraintEqualToAnchor(b.widthAnchor, { multiplier: 0.5, constant: -8 }) |