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:
so why the yellow view is located there?
When it comes to bounds, the origin point is the top-left side of the subview itself, which in its own coordinate system is always equal to zero (0, 0). The width and height express the actual size of the view, and that remains always constant, regardless of any transformation that might have been applied to the view.
The green rectangle represents the view's bounds.The red dot in both images represents the origin of the frame or bounds. Frame origin = (0, 0) width = 80 height = 130 Bounds origin = (0, 0) width = 80 height = 130. So the frame and bounds were exactly the same in that picture.
At its simplest, a view's bounds refers to its coordinates relative to its own space (as if the rest of your view hierarchy didn't exist), whereas its frame refers to its coordinates relative to its parent's space.
The bounds of a view describes the views location and size in its own coordinate system. This is what the Apple documentation says: The bounds rectangle, which describes the view's location and size in its own coordinate system. Even when rotate the x , y , width and height will stay the same.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With