When to use frame and when to use bounds in iOS

Pratima S
2 min readMay 9, 2020

--

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.

Since frame relates a view’s location in its parent view, you use it when you are making outward changes, like changing its width or finding the distance between the view and the top of its parent view.

Use the bounds when you are making inward changes, like drawing things or arranging subviews within the view. Also use the bounds to get the size of the view if you have done some transformation on it

This means a few things:

  1. If you create a view at X:0, Y:0, width:100, height:100, its frame and bounds are the same.
  2. If you move that view to X:100, its frame will reflect that change but its bounds will not. Remember, the bounds is relative to the view’s own space, and internally to the view nothing has changed.
  3. If you transform the view, e.g. rotating it or scaling it up, the frame will change to reflect that, but the bounds still won’t — as far as the view is concerned internally, it hasn’t changed.

When you change the width or height of either frame or bounds, the other value is updated to match. Generally it’s better to modify bounds plus center and transform, and let UIKit calculate the frame for you.

--

--

No responses yet