以编程方式自动调整蒙版大小与 Interface Builder / xib / nib
我有一个(可能是错误的)假设,即在 xib 中启用右边距指示器相当于在代码内使用 UIViewAutoresizingFlexibleLeftMargin
等等。
所以,我曾经根据这个快照思考:
今天晚些时候,我不得不进行交叉检查,并偶然发现此线程。
还有苹果文档,标题为“使用自动调整大小规则自动处理布局更改”部分,在此链接中: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html
所以我现在对如何进行更新有了新的概念以编程方式设置自动调整大小蒙版相当于 xib 设置:
场景 1: 仅设置 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
相当于:
在 XIB 中?
场景 2: 在代码中设置 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)
相当于:
在 XIB 中?
我的 2 个更新场景正确吗?我现在的理解正确吗?
I was in an (probably false) assumption that enabling the right margin indicator in xib is equivalent to using UIViewAutoresizingFlexibleLeftMargin
inside code and so on.
So, I used to think according to this snapshot:
Later today I had to cross check, and stumbled upon this thread.
And also the apple documentation, entitled with the section with title - "Handling Layout Changes Automatically Using Autoresizing Rules" in this link: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html
So I now have a renewed concept in my mind as to how setting autoresizing masks programmatically would be equivalent to xib settings:
Scenario 1:
Setting only (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
is equivalent to:
In XIB?
Scenario 2:
Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)
in code is equivalent to:
In XIB?
Are my 2 renewed scenarios correct? Am I right now in my understanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你引用的东西是正确的。另外,我同意这感觉有点倒退,因此我很欣赏你的帖子。
在使 UIView 的边距在各个方向上灵活时,您可能喜欢使用预处理器宏
UIViewAutoresizingFlexibleMargins
。我把它放在预编译的头文件中,这样它就可以包含在任何地方。使用 UIViewAutoresizingFlexibleMargins 将使 UI 元素保持居中,因为它不会拥抱任何一侧。要使元素随其父元素增大/缩小,请分别设置
UIViewAutoresizingFlexibleWidth
和UIViewAutoresizingFlexibleHeight
。我喜欢使用 UIViewAutoresizingFlexibleMargins ,因为我以后可以这样引用它:
而不是
我经常看到这些边距在一行上进行“或”运算, “全部”,如上面的示例所示。只是很难读。
Yes, you have cited things correctly. Also, I agree that it feels a bit backwards, so for that reason I appreciate your post.
You might like using a preprocessor Macro
UIViewAutoresizingFlexibleMargins
when making a UIView's margin flexible in every direction. I put this in the precompiled header file so it gets included everywhere.Using
UIViewAutoresizingFlexibleMargins
will make a UI Element stay centered since it will NOT be hugging any one side. To make the element grow / shrink with its parent, set theUIViewAutoresizingFlexibleWidth
andUIViewAutoresizingFlexibleHeight
respectively.I like using
UIViewAutoresizingFlexibleMargins
because I can later reference it like:instead of
All to often I see these margins OR'ed together on one line like the example above. Just hard to read.
是的,Interface Builder 在某种意义上将其“反转”(或 UIView,取决于您如何看待它)。您引用的“场景”是正确的。
Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). Your cited "scenarios" are correct.
启用框内的垂直/水平箭头(称为弹簧) 将使高度/宽度灵活。但是启用外线(称为支柱)将使该侧变得不灵活/不灵活。
启用外部左线(左支柱)并不等同于启用
UIViewAutoresizingFlexibleRightMargin
。相反,如果禁用右支柱,则UIViewAutoresizingFlexibleRightMargin
= on;如果启用右支柱,则 off。乍一看很令人困惑,但如果你仔细观察,就会发现弹簧和支柱之间存在差异。我不知道苹果为什么这样做,但对我来说,在某些情况下它更容易使用。在代码中使用相反的属性会更加令人困惑。
Enabling the vertical/horizontal arrow (called spring) inside the box will make the height/width flexible. But enabling an outside line (called strut) will make that side inflexible/ non-flexible.
Enabling the outer left line (left strut) is not equivalent to enabling
UIViewAutoresizingFlexibleRightMargin
. Instead,UIViewAutoresizingFlexibleRightMargin
= on if right strut disabled, off if right strut enabled.It is quite confusing at first, but if you see closely, there is a difference in the springs and struts. I don't know why Apple did this, but for me, there were some cases where it was easier to use. And using opposite properties in code is even more confusing.
斯威夫特4
使用这个
Objective-C
Swift 4
use this
Objective-C