Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there an frame rectangle and an bounds rectangle in an UIView?

Tags:

ios

iphone

Well although it's late in the dark night, I don't get it why there are two different rectangles: frame and bounds.

Like I understand it, one single rectangle would have been just enough to do everything. Positioning the View itself relative to another coordinate system, and then clipping it's content to a specified size. What else would you do with two rectangles? And how do they interact with each other?

Does anyone have a good explanation? The one from the Apple docs with the kid holding the fruit is not very good for understanding.

like image 904
Thanks Avatar asked Apr 14 '09 22:04

Thanks


People also ask

What is the difference between frame and bounds in UIView?

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0). The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

What is frame in UIView?

Frame A view's frame ( CGRect ) is the position of its rectangle in the superview 's coordinate system. By default it starts at the top left. Bounds A view's bounds ( CGRect ) expresses a view rectangle in its own coordinate system.

What is frame and bound view?

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.

What's a difference between frame and bounds Swift?

TLDR: Bounds refers to the views own coordinate system while Frame refers to the views parent coordinate system.


3 Answers

Here's the cheatsheet:

  • frame is where the view is (with respect to the superview)
  • bounds is where the view is allowed to draw (with respect to itself)

Some more clarification:

If you are positioning the view in its superview, you almost always change the frame origin.

If you are clipping where the UIView is drawing, you almost always modify its bounds.

Note that you are allowed to have bounds that is bigger than the frame. That is, you can draw "outside the lines" of where you are.

like image 190
amattn Avatar answered Oct 11 '22 13:10

amattn


Frame is in the superview's coordinate system, bounds is in the view's coordinate system. From my perspective, it is a convenience to have both. Frame seems to be the more useful of the two, unless there is some case I am unaware of where a subview can have a completely different coordinate system (e.g. pixels scaled differently) than the superview.

like image 44
Travis Jensen Avatar answered Oct 11 '22 12:10

Travis Jensen


I've been having troubles with bounds lately and have done some experimentation. The bounds property does limit where a UIView can draw, but does not limit its subviews. The other thing bounds controls is touch event dispatching. A view will not, as far a I can tell, receive touch events that are outside its bounds. Furthermore, any subview that outside of the parent view's bounds will also not receive touch events. In these situations, you have to pretty meticulously update the bounds of the container view as the size and position of its subviews change. Everything will always draw fine (because subviews aren't clipped by the bounds of their parent) but touches won't be received.

(This really should be a reply to an earlier post, but since I can't reply yet, it's stuck here...)

like image 23
drewww Avatar answered Oct 11 '22 14:10

drewww