Ninjiacoder Swift Lover, Fullstack Developer

What’s the difference between the frame and the bounds?

As questioned in Cocoa: What's the difference between the frame and the bounds?UIView and its subclass all have the properties frame and bounds. What’s the difference?


In Apple Developer Document, it says:

The bounding rectangle is specified relative to the picture or video of the corresponding media. The rectangle’s origin is always specified in the top-left corner, and the x and y axis extend down and to the right.

So it’s relative to its own coordinate system (0, 0)


What about frame? In Apple Developer Document, it says:

The frame rectangle is position and size of the layer specified in the superlayer’s coordinate space.

So, it’s relative to the superview it is contained within.


Let’s see the example in the top answer to the question on StackOverflow.

Imagine a view that has a size of 100x100 (width x height) positioned at 25, 25 (x, y) of its superview. The following code prints out this view’s bounds and frame:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);

And the output of this code is:

bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100

frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100

So we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x, y positioning of the view. In the case of the bounds, the x and y coordinates are at 0, 0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view.

More info

In the answer, a slide about UIView has been shared.