What if a UIView has no superview, what does its f

2019-05-21 02:23发布

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.

Then what does it's frame property become?

Definition of frame I found here: "A view's frame is the position of its rectangle in the superview's coordinate system."

标签: ios uiview
2条回答
Deceive 欺骗
2楼-- · 2019-05-21 02:38

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.

If a view is visible, it must have a superview. UIWindow is also a UIView (it derives from `UIView), so if you add it to your main window, it is still a subview of a superview.

Now, if you read UIWindow reference:

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

or 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

So, a UIWindow frame is defined in terms of the display coordinate system.

And from 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.

Generally speaking, what is a view's frame depends on the view and how it is created. When you create a view, you normally use:

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

so, the view's frame is the passed CGRect argument, even when the view is not added to any superview.

When you use addSubview to add that view to some other view, then the information you specified when instantiating it is used to determine the position of the added view in the superview's coordinate system.

In other words, a UIView´s frame is just an attribute of the view; this attribute is interpreted as the view´s geometrical position inside of its superview when the view must be displayed.

Furthermore, a view´s frame doesn't change merely by removing it from its superview. It stays the same, in case you add it to a view again. (thanks H2CO3 for the remark)

Hope this helps.

查看更多
疯言疯语
3楼-- · 2019-05-21 02:43

If you have a UIView on the screen its going to at least be in the main UIWindow of your app. So the UIVIew of your first view controller will be within the main UIWindow's frame.

查看更多
登录 后发表回答