What is the meaning of view.bounds.origin?

let's say if I set view.bounds.origin to (50,50), then the preview will be drawn (50,50), left to view. But I thought it should be the opposite, so what does bounds.origin mean?

Sorry guys, I am not a native speaker of English, so this time I put this sample code and image ~~

subview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; subview.backgroundColor = [UIColor blueColor]; subview.bounds = CGRectMake(50, 50, 200, 200); subsubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,100,100)]; subsubview.backgroundColor = [UIColor yellowColor]; [subview addSubView:subsubView]; 

this will result in this result: enter image description here

So why is there a yellow view?

+7
source share
5 answers

From the documentation :

On the screen, the border rectangle represents the same visible part of the view as its frame rectangle. By default, the origin of the border rectangle is set to (0, 0), but you can change this value to display different parts of the view.

What you did, changing bounds , effectively move the interior space of the subView coordinates down and right by 50 points. Then you added a “subsubView” with a start of 0,0 in the subView coordinate subView - that means 50 points up and to the left of the visible start of the subView .

If you set the subView frame instead of bounds , you would move the subView inside the coordinate space of the supervisor, so your blue square would move up and to the left, and the yellow square contained in it would have the same origin.

Setting bounds to something that has no origin (0,0) is like adding a translation to the view. In almost all cases, this is not what you want to do, and you should set the frame property instead. Each view in the view hierarchy has its own coordinate space. frame is where the view refers to its supervision, and bounds is the space inside the view.

So, the subview frame describes its location and size in the bounds . If superview bounds has a non-zero origin, and you add a subview with a frame that has a zero origin, it will be "outside" the boundaries of the supervisor.

+5
source

The problem is that we usually think that when changing the beginning of the borders of the blue view (for example, we shift it to the bottom right), the yellow view should also shift to the bottom right, since the position of the yellow view relative to its observation coordinate system (in this case, the reference point is blue view ) . This is not true.

Instead, we should think that the blue view defines a mask in which it displays only part of the rectangle in its coordinate space. Therefore, if we change the origin of the blue view to the lower right, it simply shifts the mask to the right from the bottom, and only the views inside this mask are displayed. And for red view it always shows that the mask is in a fixed position (the blue view does not change the beginning of the frame). Think about red view by taking the blue view mask and putting it in the position defined by the blue view frame

Just found this Ole Understanding UIScrollView , which is very useful and very clearly explains

I highly recommend reading this.

The view provides the viewing area in the plane determined by its coordinate system. A straight view rectangle describes the position and size of the visible area.

It seems that the view has dropped 100 points, and this is actually true in relation to its own coordinate system. The views of the actual position on the screen (or in its supervision, to add it more accurately) remains stationary, however, since it is determined by its frame, which has not changed

enter image description here

+3
source

Your blue viewing frame is still 100, 100, 200, 200. The ratings are 50, 50, 200, 200, but the frame does not change.

A yellow view is added to the blue view at 0, 0 within, and not in the frame. Thus, yellow appears at 50, 50, 50, 50.

Edit:

subview.bounds = CGRectMake(50, 50, 200, 200);

To:

subview.bounds = CGRectMake(100, 100, 200, 200);

and see what happens.

+1
source

On the screen, the border rectangle represents the same visible part of the view as its frame rectangle. By default, the origin of the border rectangle is set to (0, 0), but you can change this value to display different parts of the view. The size of the borders of a rectangle is related to the size of the rectangle of the frame, so changes to one affect the other. Resizing borders increases or decreases the view relative to its center point. The coordinates of the border boundaries are always indicated in points.

From the UIView Documentation .

Studying Viewing a programming guide will also be helpful.

0
source

UIWindow: frame == bounds :: ==> (0, 0, windowWidth, windowHeight)

subview: frame :: ==> (100, 100, 200, 200) on UIWindows borders [UIWindows scroll space.]

When you do this:

 subview.bounds = CGRectMake(50, 50, 200, 200); 

You say that a subview subview frame will start at (50.50, 200, 200) if that subview subview [subsubview] should have properly covered the subview.

Just as you will need to specify your view frame (0,0, windowWidth, windowHeight) to properly cover the window with your view.

The bottom line is that the SUBVIEWS frame depends on its immediate SUPERVIEWS rope system. The local / internal coordinate system of direct observation is its boundaries.

To summarize why this is a subtask and takes 50 50 points.

 subview.bounds = CGRectMake(50, 50, 200, 200); subsubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,100,100)]; 

compare between (0,0) and (50,50).
We said that the local / inner coordinate space is suspended (50.50, 200, 200) Then we added a subview subview [sububview] to (0,0) inside the subview that starts with (50,50).

In this case (0,0) is located on the upper left of (50,50). That is why it happened.

If you do not understand what this means, even now, take a deep look and slowly draw rectangles in a notebook. It is difficult to make it first hand.

0
source

All Articles