ios - 如果 UIView 没有 superview,它的frame 属性会变成什么?

标签 ios uiview

假设您的屏幕上有一个 UIView,仅此而已。它没有嵌套在其他 UIView 中,没有 super View ,它独自存在。

那么它的frame属性变成了什么?

我在这里找到了框架的定义:“ View 的框架是其矩形在 super View 坐标系中的位置。”

最佳答案

Let's say you have one UIView on your screen and that's it. It's not nested in some other UIView, there are no superviews, it's there by its lonesome.

如果一个 View 可见,它必须有一个 super View 。 UIWindow也是UIView (它派生自“UIView”),因此如果将其添加到主窗口,它仍然是 super View 的 subview 。

现在,如果您阅读UIWindow引用:

The UIWindow class defines an object known as a window that manages and coordinates the views an app displays on a device screen.

here :

In iOS, the window object contains an app’s views and manages their presentation on the device display. The window’s associated screen object represents the specific device display currently in use. If

所以,一个UIWindow框架是根据显示坐标系定义的。

来自 UIScreen reference :

A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up your application’s user interface, you should use the properties of this object to get the recommended frame rectangles for your application’s window.

一般来说,什么是 View 的框架取决于 View 及其创建方式。创建 View 时,通常使用:

[[UIView alloc] initWithFrame:...];

所以, View 的框架是传递的CGRect参数,即使 View 没有添加到任何 super View 中。

当您使用addSubview时将该 View 添加到其他 View ,然后实例化它时指定的信息将用于确定添加的 View 在 super View 坐标系中的位置。

换句话说,UIView 的框架只是 View 的一个属性;当必须显示 View 时,此属性被解释为 View 在其父 View 内的几何位置。

此外, View 的框架不会仅仅通过将其从其 super View 中删除而改变。如果您再次将其添加到 View 中,它会保持不变。 (感谢H2CO3的评论)

希望这有帮助。

关于ios - 如果 UIView 没有 superview,它的frame 属性会变成什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244040/

相关文章:

ios - 我是否必须升级到 Lion 才能在运行 iOS 5.1 的 iPad 上进行测试?

ios - 在 Storyboard 上工作时 Storyboard UI 不更新

ios - UIButton 没有添加到 UIView

swift - TableView Section Footers 在旋转后不会调整其宽度

iOS:如何确定 UIView 中 CGRect 中最常见的颜色?

ios - 在原型(prototype)单元格模式下在 UITableView 中添加行(这不是我的期望)

ios - 如何在 UITableView 底部添加 UIRefreshControl?

iOS 扩展和访问资源中的文件

ios - 为什么当我删除 UIView subview 时我的 SurperView 没有响应?

ios - Object C中UIView初始显示的机制是什么