UIView:layoutMargins 属性
layoutMargins 属性是视图的内边距,左右写死为屏幕的左和右。它和 directionalLayoutMargins 是同一份数据。
值
UIEdgeInsets,单位 pt。字段是 top、left、bottom、right。
- 可读,可写。
- 默认值是
(8, 8, 8, 8)。 - 写其中一个,
directionalLayoutMargins跟着变。界面要支持从右往左的语言时,用directionalLayoutMargins。 - 读出来的值会加上安全区,规则和
directionalLayoutMargins相同,见insetsLayoutMarginsFromSafeArea。 - 写入时可以给
UIEdgeInsets,也可以给四个字段齐全的普通对象。读出来的结构体字段只读,每次读得到一个新对象,见结构体。 - 没有变化通知,原因同
directionalLayoutMargins。
写错时
- 给的不是对象:抛
TypeError: layoutMargins needs an object with all of: top, left, bottom, right。 - 缺字段,或字段转不出有限的数:抛
TypeError: layoutMargins is missing …, or it is not a finite number,中间是缺的字段名。 - 出错时属性保持原值。字段的值按
Number()转换,所以'10'可以。
示例
js
const box = new UIView();
box.layoutMargins = { top: 5, left: 6, bottom: 7, right: 8 };
console.log(String(box.directionalLayoutMargins));结果:语言是从左往右时,输出 NSDirectionalEdgeInsets(5, 6, 7, 8)。
对应的 iOS API
UIView.layoutMargins。UIEdgeInsetsMake(8, 16, 8, 16) 写成 new UIEdgeInsets(8, 16, 8, 16)。