gpt4 book ai didi

ios - 如果 UIView 没有父 View 怎么办,它的 frame 属性会变成什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:14 27 4
gpt4 key购买 nike

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

那么它的frame属性会变成什么呢?

我在这里找到的框架定义:“ View 的框架是它的矩形在父 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 是可见的,它必须有一个父 View 。 UIWindow 也是一个 UIView(它派生自 `UIView),所以如果将它添加到主窗口,它仍然是父 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 未添加到任何父 View 也是如此。

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

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

此外, View 的框架不会仅仅通过将其从其父 View 中移除而改变。它保持不变,以防您再次将它添加到 View 中。 (感谢H2CO3的点评)

希望这对您有所帮助。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com