UIView Frames and Bounds

Posted on by in Development

All UIViews have frame and bounds properties which define their dimensions. The similarity of these properties can cause some confusion when attempting to determine which property to use to calculate view sizes or reposition views.

The frame of a view is given in the coordinates of its superview so a view controller’s view’s frame will include offsets for the status bar, navigation bar, or tab bar. The bounds of a view are given in the view’s own coordinate system which does not include these elements.

In addition the frame property’s values are undefined if the view has any transform other than the identity transform. Rotating a view into landscape mode applies a transform to the view so it is not safe to rely on frame values for an app in landscape mode.

When a transform is applied to a view it does not apply to any subviews. A UIViewController or custom UIView subclass is therefore free to adjust the frames of its subviews to reposition them regardless of the device’s orientation. For example a UIView class can change the frames of its subviews within its layoutSubviews method no matter what transform might have been applied to the parent view.
Each view’s frame will position it relative to its parent view so an origin of (0, 0) will always place a subview in the top left corner of its parent view.

See the images below for examples

This view controller's view has a frame of (x:0.00 y:20.00 width:320.00 height:460.00) and bounds of (x:0.00 y:0.00 width:320.00 height:460.00). Its subviews are positioned relative to their parent views and not relative to the overall window.

This view controller's view has been transformed and its frame is therefore undefined and should not be used. Its bounds are (x:0.00 y:0.00 width:480.00 height:300.00). Its subviews have not been transformed so their frames can be used to position them relative to their parent views.