Bounds of a view in lifecycle methods

I've encountered with this explanation of viewDidLoad method: _ "This is called only once after creating the view and loading it into memory. But the bounds of the view are not defined yet. We generally override this method to initialize the objects which the view controller will use. We shouldn’t forget to call super when we override." _ I decided to check this, and the only time I didn't get the final bounds of a root view is when overrided loadView method and swapped the root view with custom view without specifying its frame. And the size of the subviews of the root view are only available after viewDidLayoutSubviews if I'm not mistaken.

Accepted Reply

When UIKit builds the default view for a view controller (either in code, or loaded from a storyboard) there is a default size that is used, and for many use cases it is often true that this size is the same as the final layout size for that view. However there are just as many cases where that is not true such as:

  1. Your view controller will be presented in landscape orientation
  2. Your view controller will be presented in a non-fullscreen presentation
  3. Your view controller will be nested in another container view controller (such as a split view controller).

There are other cases as well, but those are the most common ones. The only reason why the bounds of the view is even non-zero inside of viewDidLoad is because many layouts rely on a non-zero initial size (such as those that exclusively use autoresizing masks).

Replies

When UIKit builds the default view for a view controller (either in code, or loaded from a storyboard) there is a default size that is used, and for many use cases it is often true that this size is the same as the final layout size for that view. However there are just as many cases where that is not true such as:

  1. Your view controller will be presented in landscape orientation
  2. Your view controller will be presented in a non-fullscreen presentation
  3. Your view controller will be nested in another container view controller (such as a split view controller).

There are other cases as well, but those are the most common ones. The only reason why the bounds of the view is even non-zero inside of viewDidLoad is because many layouts rely on a non-zero initial size (such as those that exclusively use autoresizing masks).