UIView的autoresizingMask的目的是什么?

发布于 2024-12-03 07:32:36 字数 238 浏览 2 评论 0原文

在阅读了关于 SO 和 developer.apple.com 我仍然不清楚目的是什么。什么情况下需要设置这个属性?

After reading about UIView's autoresizingMask on SO and developer.apple.com I'm still unclear what the purpose is. What's a situation where setting this property is necessary?

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

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

发布评论

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

评论(2

寄居者 2024-12-10 07:32:36

是的,如果您不想手动调整视图大小,通常需要设置它。请注意,它对于子视图(即那些不占据整个屏幕的视图)最有用,而不是应用程序的主视图。

在以下情况下,视图通常可能需要调整大小:

  • 设备旋转
  • 额外视图(例如广告)添加到视图中,因此现有子视图的可用空间较少。

例如,假设您的视图上有两个按钮,一个位于左上角,另一个位于右上角。为了使按钮在视图从纵向转换为横向时变宽,需要将FlexibleLeftMargin 设置为右侧按钮,将FlexibleRightMargin 设置为左​​侧按钮。

编辑:当设备旋转或添加新的子视图时,如果您看到奇怪的孔或重叠,autoresizingMask 也是首先要查看的。通常,对子视图的这些蒙版进行正确设置可以让您在两个方向上获得漂亮的视图,而无需手动布局子视图 - 但通常需要进行一些试验。

Edit2:(因为这仍在收集赞成票)自动调整大小蒙版现在大多被“自动布局”取代,它允许对视图的大小和位置进行更灵活的限制。话虽如此,translatesAutoresizingMaskIntoConstraints 对于动态添加的视图偶尔仍然有用。

Yes, it is often necessary to set it if you don't want to resize the views manually. Note that it is mostly useful for subviews (i.e. those views that don't take the whole screen) rather then the main view of your app.

Views typically may need resizing if:

  • the device is rotated
  • an extra view (say, an ad) is added to the view, so the existing subviews have less available space.

For example, suppose if you have a view with two buttons on it, one in the top-left corner, another in the top-right corner. In order for the buttons to get wider when the view transitions from portrait to landscape, you need to set the FlexibleLeftMargin to the right button, FlexibleRightMargin to the left button.

Edit: autoresizingMask is also the first thing to look at if you see weird holes or overlaps when device is rotated or a new subview is added. Quite often the proper setting of these masks for subviews can get you a nice looking view in both orientations without having to lay out subviews manually - but usually it takes some experimenting.

Edit2: (since this is still gathering upvotes) Autoresizing masks are now mostly superseded with "Auto Layout", which allows for much more flexible constraints on views' sizes and positions. That being said, translatesAutoresizingMaskIntoConstraints is still occasionally useful for dynamically added views.

§对你不离不弃 2024-12-10 07:32:36

目的是当 UIView 的超级视图由于调整大小、方向变化、在表格视图单元格中显示编辑控件等而发生变化时,UIView 会正确地移动和调整大小。

The purpose is that UIView properly shifts and resizes when its superview changes due to resizing, orientation change, showing editing controls in tableview cells etc.

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