Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to determine UIView size
    primarykey
    data
    text
    <p>Summary: How should the <code>UIViewController</code> know the size of its UIView instance when initialising that view?</p> <p>The dedicated initialisation method for an UIView is the <code>initWithFrame:(CGRect)frame</code> method. This sets the frame for the newly created UIView. This method could be called from the <code>UIViewController's</code> loadView method, if that view controller's view is requested. The documentation of <code>UIViewController</code> states with respect to subclassing and view size:</p> <blockquote> <p>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.</p> </blockquote> <p>So, the <code>UIViewController</code> instance should set those properties. So far so good, the <code>UIViewController</code> so far does not have to know how big its view is or will be.</p> <p>When the view of a <code>UIViewController</code> is requested and the view property is nil, the loadView method of the view controller is called. Now there is a problem, because the <code>UIView</code> 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?</p> <p>You could initialize the view with a zero rect (<code>CGRectZero</code>):</p> <pre><code>- (void)loadView { self.view = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; } </code></pre> <p>And let the caller set the view frame like so:</p> <pre><code>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); </code></pre> <p>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 <code>CGRectZero</code>. Then the caller sets its frame (view.frame = ...)</p> <p>The thing is that the frame property on the view is set twice, possibly causing even more double work if your custom <code>UIView</code> 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 <code>UIViewController</code> 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.</p> <p>What is the good way to go here? Either setting the view's frame twice (initializing with <code>CGRectZero</code>, and setting afterwards), or giving the <code>UIViewController</code> a new initializer method with a CGRect (and thus giving it a frame property)? Or am I missing something and are there other possibilities?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload