Consider a view controller that needs to slide out (or hide) the status bar when a button is clicked.
- (void) buttonClick:(id)sender
{
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationSlide];
}
The above effectively hides the status bar, but does not resize the root view appropriately, leaving a 20 pixel gap on top.
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).
What's the proper way of doing this?
(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)
Obviously, the following works...
[[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;
}];
...but has disadvantages:
UIViewAutoresizingFlexibleTopMargin
and UIViewAutoresizingFlexibleHeight
.[self.view setNeedsLayout]
after hiding the status bar.[self.view setNeedsDisplay]
after hiding the status bar.wantsFullScreenLayout
to YES
before and after hiding the status bar.This works fine and has nothing hard coded.
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
self.navigationController.navigationBar.frame = self.navigationController.navigationBar.bounds;
self.view.window.frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height);
}];