简体   繁体   English

以编程方式对Interface Builder / xib / nib自动调整掩码

[英]Autoresizing masks programmatically vs Interface Builder / xib / nib

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/ 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: 场景1 :仅设置(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)相当于:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)

In XIB? 在XIB?

Scenario 2 : Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) in code is equivalent to: 场景2 :代码中的设置(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)相当于:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)

In XIB? 在XIB?

Are my 2 renewed scenarios correct? 我的2个更新方案是否正确? Am I right now in my understanding? 我现在理解了吗?

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. 在使UIView的边距在各个方向都灵活时,您可能会喜欢使用预处理器Macro UIViewAutoresizingFlexibleMargins I put this in the precompiled header file so it gets included everywhere. 我把它放在预编译的头文件中,所以它包含在任何地方。

#define UIViewAutoresizingFlexibleMargins                 \
              UIViewAutoresizingFlexibleBottomMargin    | \
              UIViewAutoresizingFlexibleLeftMargin      | \
              UIViewAutoresizingFlexibleRightMargin     | \
              UIViewAutoresizingFlexibleTopMargin

Using UIViewAutoresizingFlexibleMargins will make a UI Element stay centered since it will NOT be hugging any one side. 使用UIViewAutoresizingFlexibleMargins将使UI元素保持居中,因为它不会拥抱任何一方。 To make the element grow / shrink with its parent, set the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleHeight respectively. 要使元素与其父元素一起增长/缩小, UIViewAutoresizingFlexibleHeight分别设置UIViewAutoresizingFlexibleWidthUIViewAutoresizingFlexibleHeight

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. 只是很难读。

Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). 是的,Interface Builder在某种意义上使它“逆转”(或UIView,取决于你如何看待它)。 Your cited "scenarios" are correct. 您引用的“情景”是正确的。

在此输入图像描述

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 . 启用左外侧行(左侧支柱)并不等同于启用UIViewAutoresizingFlexibleRightMargin Instead, UIViewAutoresizingFlexibleRightMargin = on if right strut disabled, off if right strut enabled. 相反,如果右侧支柱被禁用, 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. 我不知道Apple为什么会这样做,但对我来说,有些情况下它更容易使用。 And using opposite properties in code is even more confusing. 在代码中使用相反的属性更加令人困惑。

Swift 4 use this Swift 4使用它

gadBannerView?.autoresizingMask = [.flexibleRightMargin  , .flexibleLeftMargin , .flexibleTopMargin , .flexibleBottomMargin]

Objective-C Objective-C的

myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM