Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSView's bounds vs frame

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.

like image 655
AWF4vk Avatar asked Dec 13 '11 21:12

AWF4vk


People also ask

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.

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.

Are bounds origin always zero?

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.

What is a frame in Uikit?

The frame rectangle, which describes the view's location and size in its superview's coordinate system.


2 Answers

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:

enter image description here

like image 173
Dan Avatar answered Sep 27 '22 17:09

Dan


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.

like image 30
tagirkaZ Avatar answered Sep 27 '22 17:09

tagirkaZ