title: Proper way to hide status bar on iOS, with animation and resizing root view
body: <p>Consider a view controller that needs to slide out (or hide) the status bar when a button is clicked.</p>
  
  <pre><code>- (void) buttonClick:(id)sender
  {
      [[UIApplication sharedApplication] setStatusBarHidden:YES
                                              withAnimation:UIStatusBarAnimationSlide];
  }
  </code></pre>
  
  <p>The above effectively hides the status bar, but does not resize the root view appropriately, leaving a 20 pixel gap on top.</p>
  
  <p>What I expected is the root view to expand over the space that was previously used by the status bar (animated, with the same duration than the status bar animation).</p>
  
  <p>What's the proper way of doing this?</p>
  
  <p><em>(I'm aware there are plenty of similar questions, but I couldn't find any about hiding the status bar on demand as opposed to hiding it to display a new view controller)</em></p>
  
  <h1>The "brute force" approach</h1>
  
  <p>Obviously, the following works...</p>
  
  <pre><code>[[UIApplication sharedApplication] setStatusBarHidden:YES
                                          withAnimation:UIStatusBarAnimationSlide];
  [UIView animateWithDuration:0.25 animations:^{
      CGRect frame = self.view.frame;
      frame.origin.y -= 20;
      frame.size.height += 20;
      self.view.frame = frame;
  }];
  </code></pre>
  
  <p>...but has disadvantages:</p>
  
  <ul>
  <li>Hardcodes the duration of the slide animation</li>
  <li>Hardcodes the height of the status bar</li>
  <li>The root view origin stays at (0,-20). I like my frames to start at (0,0) whenever possible.</li>
  </ul>
  
  <h1>What I already tried</h1>
  
  <ul>
  <li>Made sure the autoresize mask of the root view has <code>UIViewAutoresizingFlexibleTopMargin</code> and <code>UIViewAutoresizingFlexibleHeight</code>.</li>
  <li>Called <code>[self.view setNeedsLayout]</code> after hiding the status bar.</li>
  <li>Called <code>[self.view setNeedsDisplay]</code> after hiding the status bar.</li>
  <li>Set <code>wantsFullScreenLayout</code> to <code>YES</code> before and after hiding the status bar.</li>
  </ul>