ios UIView autoresizingMask - Interface Builder to Code - 以编程方式创建 struts 和 springs - Swift 或 Objective-C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10468389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C
提问by MattyG
I've laid out some subviews with Interface Builder, but I'd like to do it in code instead.
我已经用 Interface Builder 布置了一些子视图,但我想用代码来代替。
I've read the UIView docsabout 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).
我已阅读有关设置 view.autoresizingMask 属性的 UIView 文档。我正在寻找有关如何使用提供的各种掩码(例如UIViewAutoresizingFlexibleLeftMargin
,等)翻译支柱和弹簧的合乎逻辑的解释。
回答by MattyG
When setting the autoresizing mask for a view, use a bitwise inclusive OR (|
) (Objective-C), or an array (Swift 2, 3, 4) to specify springsand struts.
为视图设置自动调整大小掩码时,使用按位包含 OR ( |
) (Objective-C) 或数组 (Swift 2, 3, 4) 来指定springs和struts。
Springsare represented by specifying a mask (Objective-C or Swift, respectively):
vertical spring:
UIViewAutoresizingFlexibleHeight
or.flexibleHeight
horizontal spring:
UIViewAutoresizingFlexibleWidth
or.flexibleWidth
Strutsare 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
Springs通过指定掩码(分别是Objective-C 或Swift)来表示:
垂直弹簧:
UIViewAutoresizingFlexibleHeight
或.flexibleHeight
水平弹簧:
UIViewAutoresizingFlexibleWidth
或.flexibleWidth
Struts表示缺少四个“灵活边距”掩码之一(即,如果一个 strut 不存在,则指定该边距的掩码):
UIViewAutoresizingFlexibleLeftMargin
或者.flexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
或者.flexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
或者.flexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
或者.flexibleBottomMargin
For example, a view with a horizontal springand top and bottom strutswould have the width, and left and right margins specified as flexible:
例如,具有水平弹簧和顶部和底部支柱的视图将具有指定为灵活的宽度和左右边距:
Swift 3 and 4
斯威夫特 3 和 4
mySubview.autoresizingMask = [.flexibleWidth, .flexibleLeftMargin, .flexibleRightMargin]
Swift 2
斯威夫特 2
mySubview.autoresizingMask = [.FlexibleWidth, .FlexibleLeftMargin, .FlexibleRightMargin]
Swift 1.2
斯威夫特 1.2
mySubview.autoresizingMask = .FlexibleWidth | .FlexibleLeftMargin | .FlexibleRightMargin
Objective-C
目标-C
mySubview.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin);
回答by CodaFi
UIViewAutoResizingMask
s are what we refer to as 'struts' and 'springs'. Consider this: you have a large square with a small square inside. In order for that square to stay perfectly centered, you must set a fixed width from each inside edge of the large square, so as to constrain it. These are struts.
UIViewAutoResizingMask
s 是我们所说的“支柱”和“弹簧”。考虑一下:你有一个大正方形,里面有一个小正方形。为了使该正方形保持完美居中,您必须从大正方形的每个内边缘设置固定宽度,以便对其进行约束。这些是支柱。
Springs, on the other hand, work more like a UIView
does during rotation. Let's say our view must stay on the bottom of the screen, aligned in the center (Much like a UIToolbar
). We want to keep it's Top spring flexible so that when the view rotates from 460 px to 320 px, it keeps it's same position relative to the screen's now changed dimensions. Highlighting one of those springs in IB is equal to setting the appropriate UIViewAutoResizingMask
and highlighting the top spring specifically is akin to calling myView.autoResizingMask = UIViewAutoresizingFlexibleTopMargin
.
另一方面,弹簧UIView
在旋转过程中更像是在工作。假设我们的视图必须位于屏幕底部,居中对齐(很像 a UIToolbar
)。我们想让它的 Top spring 保持灵活,这样当视图从 460 px 旋转到 320 px 时,它相对于屏幕现在更改的尺寸保持相同的位置。在 IB 中突出显示这些弹簧中的一个等同于设置适当的,UIViewAutoResizingMask
并且特别突出显示顶部弹簧类似于调用myView.autoResizingMask = UIViewAutoresizingFlexibleTopMargin
.
Values may be used in tandem by enclosing them in a pair of parenthesis and using an or operator like myView.autoResizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)
值可以通过将它们括在一对括号中并使用 or 操作符来串联使用 myView.autoResizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin)
The masks are reporting numbers to you because they are a typdef of an NSUInteger
and those are the flags that apple has assigned them. Cmd+click on one to see its method definition.
掩码向您报告数字,因为它们是 an 的类型定义,NSUInteger
而这些是 Apple 分配给它们的标志。Cmd+单击一个以查看其方法定义。