UIView:heightAnchor 属性
heightAnchor 属性是视图的高度,用来建约束。
值
- 只读。给它赋值抛
TypeError。 - 同一个视图上多次读取得到同一个对象:
view.heightAnchor === view.heightAnchor是true。 - 锚点不能自己构造。代码里只保存了锚点、没有保存视图时,锚点照样能用。
- 只能和同一种类型的锚点建约束,见
NSLayoutAnchor。 - 尺寸锚点能和常数比,也能带倍数。
示例
js
const parent = new UIView();
const box = new UIView();
box.translatesAutoresizingMaskIntoConstraints = false;
parent.addSubview(box);
box.heightAnchor.constraintEqualToAnchor(box.widthAnchor, { multiplier: 9 / 16 }).isActive = true;效果:box 的高始终是宽的 9/16。未在模拟器验证。
对应的 iOS API
UIView.heightAnchor。
参见
NSLayoutDimension:能建哪些约束- 用约束排版