ios 以编程方式自动调整蒙版大小 vs Interface Builder / xib / nib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7754851/
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
Autoresizing masks programmatically vs Interface Builder / xib / nib
提问by Raj Pawan Gumdal
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.
我有一个(可能是错误的)假设,即在 xib 中启用右边距指示器等效于使用UIViewAutoresizingFlexibleLeftMargin
内部代码等。
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
还有苹果文档,标题为“使用自动调整大小规则自动处理布局更改”部分的标题:https: //developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/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:
所以我现在对如何以编程方式设置自动调整大小掩码等效于 xib 设置有了一个新的概念:
Scenario 1:
Setting only (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
is equivalent to:
场景一:只设置(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)
相当于:
In XIB?
在XIB?
Scenario 2:
Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)
in code is equivalent to:
场景二:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)
在代码中设置等价于:
In XIB?
在XIB?
Are my 2 renewed scenarios correct? Am I right now in my understanding?
我的 2 个更新场景是否正确?我现在理解了吗?
采纳答案by DarkDust
Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). Your cited "scenarios" are correct.
是的,Interface Builder 在某种意义上将它“颠倒”了(或 UIView,取决于你如何看待它)。您引用的“场景”是正确的。
回答by Sam
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.
UIViewAutoresizingFlexibleMargins
在使 UIView 的边距在各个方向上都灵活时,您可能喜欢使用预处理器宏。我把它放在预编译的头文件中,所以它被包含在任何地方。
#define UIViewAutoresizingFlexibleMargins \
UIViewAutoresizingFlexibleBottomMargin | \
UIViewAutoresizingFlexibleLeftMargin | \
UIViewAutoresizingFlexibleRightMargin | \
UIViewAutoresizingFlexibleTopMargin
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 the UIViewAutoresizingFlexibleWidth
and UIViewAutoresizingFlexibleHeight
respectively.
使用UIViewAutoresizingFlexibleMargins
将使 UI 元素保持居中,因为它不会拥抱任何一侧。要使元素与其父元素一起增长/缩小UIViewAutoresizingFlexibleWidth
,请UIViewAutoresizingFlexibleHeight
分别设置和。
I like using UIViewAutoresizingFlexibleMargins
because I can later reference it like:
我喜欢使用,UIViewAutoresizingFlexibleMargins
因为我以后可以像这样引用它:
myView.autoresizingMask = UIViewAutoresizingFlexibleMargins;
instead of
代替
myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
All to often I see these margins OR'ed together on one line like the example above. Just hard to read.
我经常看到这些边距或像上面的例子一样在一行上。只是难读。
回答by Abdurrahman Mubeen Ali
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
= onif right strut disabled, offif right strut enabled.
启用外部左线(左支柱)不等同于启用UIViewAutoresizingFlexibleRightMargin
. 相反,如果右支柱禁用,则UIViewAutoresizingFlexibleRightMargin
= on,如果右支柱启用,则关闭。
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.
一开始很混乱,但如果你仔细观察,弹簧和支柱是有区别的。我不知道苹果为什么这样做,但对我来说,在某些情况下它更容易使用。在代码中使用相反的属性更令人困惑。
回答by Muhammad Nayab
Swift 4use this
Swift 4使用这个
gadBannerView?.autoresizingMask = [.flexibleRightMargin , .flexibleLeftMargin , .flexibleTopMargin , .flexibleBottomMargin]
Objective-C
目标-C
myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;