我已经用Interface Builder布置了一些子视图,但是我想用 代码代替。
我已经阅读了 有关设置view.autoresizingMask属性的UIView 文档。我正在寻找一种逻辑解释,说明如何使用提供的各种遮罩(例如UIViewAutoresizingFlexibleLeftMargin)来平移支柱和弹簧。
UIViewAutoresizingFlexibleLeftMargin
为视图设置自动调整大小蒙版时,请使用按位包含 (或|)的OR ()(Objective-C)或数组(Swift 2、3、4)来指定springs和struts。
|
Springs are represented by specifying a mask (Objective-C or Swift, respectively):
vertical spring: UIViewAutoresizingFlexibleHeight or .flexibleHeight
UIViewAutoresizingFlexibleHeight
.flexibleHeight
horizontal spring: UIViewAutoresizingFlexibleWidth or .flexibleWidth
UIViewAutoresizingFlexibleWidth
.flexibleWidth
Struts are represented by the lack of one of the four ‘flexible margin’ masks (i.e. if a strut does not exist, the mask for that margin is specified):
UIViewAutoresizingFlexibleLeftMargin or .flexibleLeftMargin
.flexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin or .flexibleRightMargin
UIViewAutoresizingFlexibleRightMargin
.flexibleRightMargin
UIViewAutoresizingFlexibleTopMargin or .flexibleTopMargin
UIViewAutoresizingFlexibleTopMargin
.flexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin or .flexibleBottomMargin
UIViewAutoresizingFlexibleBottomMargin
.flexibleBottomMargin
例如,具有水平弹簧以及顶部和底部支撑的视图 将具有宽度,并且左右边界指定为可伸缩的:
斯威夫特3和4
Swift 3 and 4
mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
Swift 2
mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]
Swift 1.2
mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin
Objective-C
mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);