以编程方式自动调整蒙版大小与 Interface Builder / xib / nib

发布于 2024-12-09 22:39:36 字数 1677 浏览 1 评论 0原文

我有一个(可能是错误的)假设,即在 xib 中启用右边距指示器相当于在代码内使用 UIViewAutoresizingFlexibleLeftMargin 等等。

所以,我曾经根据这个快照思考: 在此处输入图像描述

今天晚些时候,我不得不进行交叉检查,并偶然发现此线程

还有苹果文档,标题为“使用自动调整大小规则自动处理布局更改”部分,在此链接中: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html

所以我现在对如何进行更新有了新的概念以编程方式设置自动调整大小蒙版相当于 xib 设置:

场景 1: 仅设置 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) 相当于:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)

在 XIB 中?

场景 2: 在代码中设置 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) 相当于:

( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | 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:
enter image description here

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:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)

In XIB?

Scenario 2:
Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) in code is equivalent to:

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

In XIB?

Are my 2 renewed scenarios correct? Am I right now in my understanding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゝ偶尔ゞ 2024-12-16 22:39:36

是的,你引用的东西是正确的。另外,我同意这感觉有点倒退,因此我很欣赏你的帖子。

在使 UIView 的边距在各个方向上灵活时,您可能喜欢使用预处理器宏 UIViewAutoresizingFlexibleMargins 。我把它放在预编译的头文件中,这样它就可以包含在任何地方。

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

使用 UIViewAutoresizingFlexibleMargins 将使 UI 元素保持居中,因为它不会拥抱任何一侧。要使元素随其父元素增大/缩小,请分别设置 UIViewAutoresizingFlexibleWidthUIViewAutoresizingFlexibleHeight

我喜欢使用 UIViewAutoresizingFlexibleMargins ,因为我以后可以这样引用它:

myView.autoresizingMask = UIViewAutoresizingFlexibleMargins;

而不是

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

我经常看到这些边距在一行上进行“或”运算, “全部”,如上面的示例所示。只是很难读。

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.

#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.

I like using UIViewAutoresizingFlexibleMargins because I can later reference it like:

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.

暮色兮凉城 2024-12-16 22:39:36

是的,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.

浊酒尽余欢 2024-12-16 22:39:36

输入图片此处描述

启用框内的垂直/水平箭头(称为弹簧) 将使高度/宽度灵活。但是启用外线(称为支柱)将使该侧变得不灵活/不灵活。

启用外部左线(左支柱)并不等同于启用UIViewAutoresizingFlexibleRightMargin。相反,如果禁用右支柱,则 UIViewAutoresizingFlexibleRightMargin = on;如果启用右支柱,则 off

乍一看很令人困惑,但如果你仔细观察,就会发现弹簧和支柱之间存在差异。我不知道苹果为什么这样做,但对我来说,在某些情况下它更容易使用。在代码中使用相反的属性会更加令人困惑。

enter image description here

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.

甜柠檬 2024-12-16 22:39:36

斯威夫特4
使用这个

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

Objective-C

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

Swift 4
use this

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

Objective-C

myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文