When would an UIView's bounds.origin
not be (0, 0)
?
This post was helpful to me:
IMPORTANT!! Bounds X and Y, the origin, are for moving inside the View. For eample X+5, moving 5pix to the left of the frame's origin meaning draw all content within this View to the left 5pix of frame's origin. It doesn't do anything to itself, it is what being drew on it that get affected.
But it describes only the case when I had set the value of bounds.origin
myself.
In what other cases the value of bounds.origin != (0, 0)
?
The bounds.origin will be negative if you initialize a view with negative width/height. For example, if you did
the frame would be:
bounds:
center:
try it for yourself!
A UIScrollView's bounds.origin will not be (0, 0) when its contentOffset is not (0, 0).
(edited again because I can’t delete my original answer after it was accepted—credit for this goes to ian, who posted a more thorough answer below:)
In most cases this won’t happen. If you initialize your view with a negative width and/or height, you’ll get an origin with a negative X of the width and/or negative Y of the height.
View's frame determines its location in superview. View's bounds determines its subviews locations. That means, if you change view's bounds, its location won't be changed, but all of its subviews location will be changed.
Positive width and height is like you draw a view from upper-left to bottom-right, while negative value is from bottom-right to upper-left. So
is totally identical with
And in fact, if you init a view with frame1, it will AUTOMATICALLY CHANGED to frame2.
But the
bounds.origin
of the views are not identical.Bounds.origin
indicates the point that you "draw" the view, so all subviews frames will originate at this point.For example, in Landscape iPhone 6, we have:
And we got:
We will find that rightView's frame is automatically changed to positive value, which is (342, 50, 275, 275), but its
bounts.origin = (-275,-275)
.And we add subviews:
So the bounds makes rightView's subview follows the origin which we init rightView.
If we change the bounds of rightView equals to leftView: rightView.bounds = leftView.bounds; Then the two subViews location is the same, we lost the information that rightView's width and height are negative.
And we change the bounds of leftView instead of rightView: CGRect bounds = leftView.bounds; bounds.origin = CGPointMake(50, 50); leftView.bounds = bounds;
We can see, its subview's frame.origin is offset by bounds.origin(using minus, not plus).
To conclude:
view.bounds
determines all its subview's location(offset by bounds.origin), whilebounds
will not affect its own location in its superview.If you init a view with negative width and height, it will automatically changed to positive(which won't change the location), but its bounds.origin indicates the point that you start to "draw" the view.