如何以编程方式使用 UIView autoresizingMask 属性? [英] How to use UIView autoresizingMask property programmatically?
问题描述
我已经用 Interface Builder 布置了一些子视图,但我想用代码来代替.
I've laid out some subviews with Interface Builder, but I'd like to do it in code instead.
我已经阅读了 UIView 文档 关于设置 view.autoresizingMask
属性.我正在寻找有关如何使用提供的各种掩码(例如,UIViewAutoresizingFlexibleLeftMargin
等)翻译 struts 和 springs 的逻辑解释.
I've read the UIView docs about setting the view.autoresizingMask
property. I'm looking for a logical explanation of how to translate the struts and springs by using the various masks offered (e.g., UIViewAutoresizingFlexibleLeftMargin
, etc).
推荐答案
为视图设置自动调整大小掩码时,使用按位包含 OR (|
) (Objective-C) 或数组(Swift 2, 3, 4) 指定 springs 和 struts.
When setting the autoresizing mask for a view, use a bitwise inclusive OR (|
) (Objective-C), or an array (Swift 2, 3, 4) to specify springs and struts.
Springs 通过指定掩码(分别是 Objective-C 或 Swift)来表示:
Springs are represented by specifying a mask (Objective-C or Swift, respectively):
垂直弹簧:
UIViewAutoresizingFlexibleHeight
或.flexibleHeight
水平弹簧:UIViewAutoresizingFlexibleWidth
或 .flexibleWidth
Struts 表示缺少四个灵活边距"掩码之一(即,如果一个 strut 不存在,则指定该边距的掩码):
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
或.flexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
或 .flexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
或 .flexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
或 .flexibleBottomMargin
例如,具有水平弹簧和顶部和底部支柱的视图将具有指定为灵活的宽度和左右边距:
For example, a view with a horizontal spring and top and bottom struts would have the width, and left and right margins specified as flexible:
Swift 3 和 4
mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
Swift 2
mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]
Swift 1.2
mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin
目标 C
mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin);
这篇关于如何以编程方式使用 UIView autoresizingMask 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!