UIView的autoresizingMask的目的是什么?
在阅读了关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果您不想手动调整视图大小,通常需要设置它。请注意,它对于子视图(即那些不占据整个屏幕的视图)最有用,而不是应用程序的主视图。
在以下情况下,视图通常可能需要调整大小:
例如,假设您的视图上有两个按钮,一个位于左上角,另一个位于右上角。为了使按钮在视图从纵向转换为横向时变宽,需要将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:
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.
目的是当 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.