一尘不染

UIView autoresizingMask-代码的界面生成器-以编程方式创建支杆和弹簧-Swift或Objective-C

swift

我已经用Interface Builder布置了一些子视图,但是我想用
代码代替。

我已经阅读了 有关设置view.autoresizingMask属性的UIView
文档。我正在寻找一种逻辑解释,说明如何使用提供的各种遮罩(例如UIViewAutoresizingFlexibleLeftMargin)来平移支柱和弹簧。


阅读 328

收藏
2020-07-07

共1个答案

一尘不染

为视图设置自动调整大小蒙版时,请使用按位包含
(或|)的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

    • horizontal spring: UIViewAutoresizingFlexibleWidth or .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

    • UIViewAutoresizingFlexibleRightMargin or .flexibleRightMargin

    • UIViewAutoresizingFlexibleTopMargin or .flexibleTopMargin

    • UIViewAutoresizingFlexibleBottomMargin or .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);
2020-07-07