Summary: How should the UIViewController know the size of its UIView instance when initialising that view?

The dedicated initialisation method for an UIView is the initWithFrame:(CGRect)frame method. This sets the frame for the newly created UIView. This method could be called from the UIViewController's loadView method, if that view controller's view is requested. The documentation of UIViewController states with respect to subclassing and view size:

When creating the views for your view hierarchy, you should always set the autoresizing properties of your views. When a view controller is displayed on screen, its root view is typically resized to fit the available space, which can vary depending on the window’s current orientation and the presence of other interface elements such as the status bar.

So, the UIViewController instance should set those properties. So far so good, the UIViewController so far does not have to know how big its view is or will be.

When the view of a UIViewController is requested and the view property is nil, the loadView method of the view controller is called. Now there is a problem, because the UIView needs to be initialised, but the view controller still does not know what size the view should be. How big should you initialize that view? Where in code do you determine the view size?

You could initialize the view with a zero rect (CGRectZero):

- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

And let the caller set the view frame like so:

UIViewController *viewController = [[MyUIViewController alloc] init];
// next two lines are normally combined, but for clarity they are not now
UIView *view = viewController.view;
view.frame = CGRectMake(0, 0
Edit
Report