What's the difference between bounds and frame? In fact, why does 'bounds' even exist? The size of 'bounds' is equal to the frame's size, and the bound's origin should always be 0,0.
TLDR: Bounds refers to the views own coordinate system while Frame refers to the views parent coordinate system.
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.
The origins of a bounds is not always zero. Remember I said that the bounds origin is the origin of the view with respect to its own coordinate system, so in the majority of cases, the bounds origin is (0, 0) . But not always. A transform on the view's coordinate system will affect its origin.
The frame rectangle, which describes the view's location and size in its superview's coordinate system.
From the View and Window Architecture Programming Guide for iOS:
A view object tracks its size and location using its frame, bounds, and center properties:
The frame property contains the frame rectangle, which specifies the size and location of the view in its superview’s coordinate system.
The bounds property contains the bounds rectangle, which specifies the size of the view (and its content origin) in the view’s own local coordinate system.
The center property contains the known center point of the view in the superview’s coordinate system.
Here is a good visualization of that explanation:
The bound's origin is not always 0,0. It's easy to understand the difference between frame and bounds, if you watch how change bounds property of UIScrollView during scrolling.
For example, you have UIScrollView with frame (0, 0, 320, 460), bounds (0, 0, 320, 460) and ContentSize (640, 460). Its frame always will be (0, 0, 320, 460), but the X coordinate of bounds will change depending on distance of scrolling.
It can be useful if you want to change something in you UIScrollView (create and remove pages diynamically for example), so you want to know distance of scrolling.
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