UIView autoresizingMask - Interface Builder to Code - 以编程方式创建struts和spring - Swift或Objective-C

Mat*_*tyG 56 objective-c uiview ios autoresizingmask swift

我已经使用Interface Builder列出了一些子视图,但我想在代码中进行.

我已经阅读了有关设置view.autoresizingMask属性的 UIView文档.我正在寻找一个合理的解释,如何通过使用提供的各种掩码(例如UIViewAutoresizingFlexibleLeftMargin,等)来翻译支柱和弹簧.

Mat*_*tyG 128

为视图设置自动调整遮罩时,使用按位包含的OR(|)(Objective-C)或数组(Swift 2,3)来指定弹簧支柱.

  • 通过指定掩码(Objective-C或Swift 3)来表示弹簧:

    • 垂直弹簧: UIViewAutoresizingFlexibleHeight.flexibleHeight

    • 水平弹簧: UIViewAutoresizingFlexibleWidth.flexibleWidth

  • Struts由缺少四个"灵活边距"掩码中的一个来表示(即,如果不存在支柱,则指定该边距的掩码):

    • UIViewAutoresizingFlexibleLeftMargin 要么 .flexibleLeftMargin

    • UIViewAutoresizingFlexibleRightMargin 要么 .flexibleRightMargin

    • UIViewAutoresizingFlexibleTopMargin 要么 .flexibleTopMargin

    • UIViewAutoresizingFlexibleBottomMargin 要么 .flexibleBottomMargin

例如,带有水平弹簧顶部和底部支柱的视图将具有宽度,左右边距指定为灵活:

斯威夫特3

mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]
Run Code Online (Sandbox Code Playgroud)

Swift 1.2

mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin
Run Code Online (Sandbox Code Playgroud)

Objective-C的

mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth |    
                              UIViewAutoresizingFlexibleLeftMargin |  
                              UIViewAutoresizingFlexibleRightMargin);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @Flink - 您的编辑错误.我已将我的回答恢复到原来的状态.请仔细阅读,尤其是:"Struts由缺少四个'灵活边距'掩模中的一个来表示(即如果不存在支柱,则指定该边距的掩模):" (3认同)

Cod*_*aFi 9

UIViewAutoResizingMasks是我们所说的'struts'和'spring'.考虑一下:你有一个大方形,里面有一个小方块.为了使该正方形保持完美居中,您必须从大正方形的每个内边缘设置固定宽度,以便约束它.这些都是支柱.

另一方面,弹簧的工作更像是UIView在旋转过程中所做的工作.假设我们的视图必须保持在屏幕的底部,在中心对齐(就像UIToolbar一样).我们希望保持它的顶部弹簧灵活,以便当视图从460像素旋转到320像素时,它保持相对于屏幕现在改变的尺寸的相同位置.在IB中突出显示其中一个弹簧等于设置相应的UIViewAutoResizingMask并突出显示顶部弹簧特别类似于调用UIViewAutoResizingMask.

通过将它们包含在一对括号中并使用或类似的运算符,可以串联使用这些值 UIView

掩码是向您报告数字,因为它们是NSUInteger的typdef,而那些是Apple为其分配的标志.Cmd +点击一个查看其方法定义.