What's the meaning of view.bounds.origin?

2020-02-26 00:13发布

say if I set view.bounds.origin to (50,50) ,then the subview is drawn (50,50) left up to view. But I thought it should be the inverse result, so what does bounds.origin mean?

sorry guys, I'm not a native English speaker,so I put this sample code and image this time~~

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 cause this result: enter image description here

so why the yellow view is located there?

5条回答
Rolldiameter
2楼-- · 2020-02-26 00:17

From the documentation:

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

What you've done by altering bounds is effectively to translate subView's internal coordinate space down and to the right by 50 points. You've then added 'subsubView' with an origin of 0,0 within subView's coordinate space - this is therefore 50 points up and to the left of the visible origin of subView.

If you had set subView's frame instead of bounds, you would have moved subView within it's superview's coordinate space, so your blue square would have moved up and to the left, and the yellow square would be contained within it and have the same origin.

Setting bounds to something that doesn't have an origin of (0,0) is similar to adding a translation to the view. In nearly all circumstances, this isn't what you want to do, and you should be setting the frame property instead. Each view in a view hierarchy has its own coordinate space. frame is where a view is in relation to its superview, and bounds is the space within a view.

So, a subview's frame describes its location and size in the superview's bounds. If the superview's bounds has a non-zero origin, and you add a subview with a frame having a zero origin, it is going to be "outside" the superview's bounds.

查看更多
迷人小祖宗
3楼-- · 2020-02-26 00:18

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

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

When you do this:

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

You are saying that subview's subview frame will start with (50,50, 200, 200) if that subview's subview [subsubview] were to cover the subview properly.

Just as you would need to specify your subview's frame to (0,0, windowWidth, windowHeight) to cover the window properly with your subview.

The point is, A SUBVIEWS frame is dependent on its immediate SUPERVIEWS cordinate system. The local/internal cordinate system of the immediate superview is its bounds.

Coming to the point why is the subsubview up and left by 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 subview's local/internal cordinate space to be of (50,50, 200, 200) We then added subview's subview [subsubview] at (0,0) inside a view subview which starts at (50,50).

In this case, (0,0) is top left of (50,50). That is why this happened.

If you dont get what it means even now, then take a deep look and draw some rectangles in the notebook slowly. Its tricky to get it right first hand.

查看更多
可以哭但决不认输i
4楼-- · 2020-02-26 00:22

The problem is we usually think that when changing the blue view bounds origin (for example shift to the right bottom), then the yellow view should shifts right bottom too, as yellow view position is relative to its superview coordinate system (in this case the blue view bounds origin). This is not the case

Instead, we should think that the blue view defines a mask in which it only shows a portion of the the rectangle in its coordinate space. So if we change the blue view bounds origin to the right bottom, it just shifts the mask to the right bottom, and only the views inside that mask is shown. And to the red view, it always shows that mask at a fixed position (blue view frame origin does not change). Think of the red view taking the blue view mask and putting at the position defined by the blue view frame

Just found this Understanding UIScrollView by Ole which is very helpful and explains it very clearly

I strongly recommend reading this

A view provides a viewport into the plane defined by its coordinate system. The viewʼs bounds rectangle describe the position and size of the visible area.

It looks as though the view has moved down by 100 points, and this is in fact true in relation to its own coordinate system. The viewʼs actual position on the screen (or in its superview, to put it more accurately) remains fixed, however, as that is determined by its frame, which has not changed

enter image description here

查看更多
仙女界的扛把子
5楼-- · 2020-02-26 00:23

Your blue view's frame is still 100, 100, 200, 200. The bounds are 50, 50, 200, 200, but the frame is unchanged.

The yellow view is added to the blue view, at 0, 0, within the bounds, not the frame. So the yellow is showing up at 50, 50, 50, 50.

Change:

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

To:

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

and see what happens.

查看更多
够拽才男人
6楼-- · 2020-02-26 00:40

On the screen, the bounds rectangle represents the same visible portion of the view as its frame rectangle. By default, the origin of the bounds rectangle is set to (0, 0) but you can change this value to display different portions of the view. The size of the bounds rectangle is coupled to the size of the frame rectangle, so that changes to one affect the other. Changing the bounds size grows or shrinks the view relative to its center point. The coordinates of the bounds rectangle are always specified in points.

From UIView's documentation.

Studying View Programming guide is also going to be helpful.

查看更多
登录 后发表回答