问题描述
我已经使用Interface Builder制作了一些子视图,但我想在代码中进行。
I've laid out some subviews with Interface Builder, but I'd like to do it in code instead.
我读过 view.autoresizingMask属性。我正在寻找如何通过使用提供的各种掩码来翻译struts和spring的逻辑解释(例如 UIViewAutoresizingFlexibleLeftMargin
等)。
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( |
)(目标 - C),或数组(Swift 2,3)指定弹簧和 struts 。
When setting the autoresizing mask for a view, use a bitwise inclusive OR (|
) (Objective-C), or an array (Swift 2, 3) to specify springs and struts.
-
弹簧通过指定遮罩(分别为Objective-C或Swift 3)来表示:
Springs are represented by specifying a mask (Objective-C or Swift 3, respectively):
-
垂直弹簧:
UIViewAutoresizingFlexibleHeight
或.flexibleHeight
水平弹簧: UIViewAutoresizingFlexibleWidth
或 .flexibleWidth
Struts 表示缺少四个灵活边距面具中的一个(即如果支柱是不存在,那个边缘的面具n被指定):
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
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);
这篇关于UIView autoresizingMask - Interface Builder to Code - 以编程方式创建struts和spring - Swift或Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!