UIView的autoresizingMask的目的是什么? [英] What is the purpose of UIView's autoresizingMask?

查看:111
本文介绍了UIView的autoresizingMask的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO和 UIView 的 autoresizingMask 之后。 apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html\">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?

推荐答案

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

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.

视图通常可能需要调整大小if:

Views typically may need resizing if:


  • 设备已旋转

  • 添加了额外的视图(例如,广告)到视图,所以现有的子视图具有较少的可用空间。

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

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.

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

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.

编辑2 :(因为这仍然是收集投票)自动调整掩码现在大多被自动布局取代,这允许对视图的大小和位置进行更灵活的约束。话虽这么说,translatesAutoresizingMaskIntoConstraints仍然偶尔对动态添加的视图有用。

(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的autoresizingMask的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆