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?
From the documentation:
What you've done by altering
bounds
is effectively to translatesubView
's internal coordinate space down and to the right by 50 points. You've then added 'subsubView' with an origin of 0,0 withinsubView
's coordinate space - this is therefore 50 points up and to the left of the visible origin ofsubView
.If you had set
subView
'sframe
instead ofbounds
, you would have movedsubView
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 theframe
property instead. Each view in a view hierarchy has its own coordinate space.frame
is where a view is in relation to its superview, andbounds
is the space within a view.So, a subview's
frame
describes its location and size in the superview'sbounds
. If the superview'sbounds
has a non-zero origin, and you add a subview with aframe
having a zero origin, it is going to be "outside" the superview's bounds.UIWindow: frame == bounds ::==> (0 , 0 , windowWidth, windowHeight)
subview: frame ::==> (100, 100, 200, 200) on UIWindows bounds [UIWindows cordinate space.]
When you do this:
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.
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.
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, asyellow view
position is relative to its superview coordinate system (in this case theblue view
bounds origin). This is not the caseInstead, 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 theblue 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 thered view
, it always shows that mask at a fixed position (blue view
frame origin does not change). Think of thered view
taking theblue view
mask and putting at the position defined by theblue view
frameJust found this Understanding UIScrollView by Ole which is very helpful and explains it very clearly
I strongly recommend reading this
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.
From UIView's documentation.
Studying View Programming guide is also going to be helpful.