title: Automatically Sizing UIView after Adding to Window
body: <p>Note: This may be a duplicate of <a href="https://stackoverflow.com/questions/2592447/subview-doesnt-autosize-when-added-to-root-view-controller">Subview Doesnt AutoSize When Added to Root View Controller</a></p>
  
  <hr>
  
  <p>I have an iPad app that switches between different views in its main window.  The view-switching code looks like this:</p>
  
  <pre><code>- (void)switchToViewController:(UIViewController*)viewController {
      if (currentViewController != viewController) {
          [currentViewController.view removeFromSuperview];
          currentViewController = viewController;
          [window addSubview:viewController.view];
      }
  }
  </code></pre>
  
  <p>The problem is that when the new view (a UISplitView) appears in landscape orientation, it is not sized to fill the entire window.  There is a large empty black space on the right.  It looks like the view is only 768 pixels wide, rather than the 1024-pixel width of the landscape window.</p>
  
  <p>If I rotate the device to portrait and then back to landscape, the view sizes itself properly.</p>
  
  <p>If the device is in portrait orientation, everything works fine.  The UISplitView also gets sized properly if it is the first view I show.  The problem only occurs if I switch to it after another view has been shown, in landscape.</p>
  
  <p>So, is there some way to force iPhone OS to resize the view after it has been added to the window?</p>
  
  <p>I've tried calling <code>sizeToFit</code>, and <code>setNeedsLayout</code>.  I've also tried setting the view's <code>bounds</code> to the window's <code>bounds</code>, and I've tried setting the <code>frame</code> to match the previous view's frame.</p>