Where to determine UIView size

23,472

You don't have to use the designated initializer. Just use init as in [[UIView alloc] init]. The designated initializer has to be used from subclasses' initializers.

On the other hand, setting the frame twice should not do much harm. Performing a lot of tasks in setFrame: is unusual. Layouting is normally done in layoutSubviews and is only performed once.

Share:
23,472
drvdijk
Author by

drvdijk

-

Updated on July 10, 2020

Comments

  • drvdijk
    drvdijk almost 4 years

    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, 200, 200);
    

    This requests the view from the view controller (viewController.view), and thus loads its view with the loadView method. This loadView method initializes the view with a CGRectZero. Then the caller sets its frame (view.frame = ...)

    The thing is that the frame property on the view is set twice, possibly causing even more double work if your custom UIView is doing some advanced layout in the setFrame method (placing and resizing subviews for example). You could prevent this by creating a dedicated initializer method for the UIViewController which already asks the caller for a CGRect, which you would store in an ivar. At the time the loadView method is called, you use this ivar to create the view.

    What is the good way to go here? Either setting the view's frame twice (initializing with CGRectZero, and setting afterwards), or giving the UIViewController a new initializer method with a CGRect (and thus giving it a frame property)? Or am I missing something and are there other possibilities?