Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to hide status bar on iOS, with animation and resizing root view

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)

The "brute force" approach

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:

  • Hardcodes the duration of the slide animation
  • Hardcodes the height of the status bar
  • The root view origin stays at (0,-20). I like my frames to start at (0,0) whenever possible.

What I already tried

  • Made sure the autoresize mask of the root view has UIViewAutoresizingFlexibleTopMargin and UIViewAutoresizingFlexibleHeight.
  • Called [self.view setNeedsLayout] after hiding the status bar.
  • Called [self.view setNeedsDisplay] after hiding the status bar.
  • Set wantsFullScreenLayout to YES before and after hiding the status bar.
like image 588
hpique Avatar asked Nov 29 '12 11:11

hpique


People also ask

How do I hide the status bar on my Iphone?

To completely hide it on the home screen, open up an app, such as Settings, and wait about three to four seconds. Press the home button again, and the status bar will be completely gone.


1 Answers

For those that are trying to implement this with view controller-based status bar appearance, you need to implement the prefersStatusBarHidden method in your view controller

 - (BOOL)prefersStatusBarHidden {     // If self.statusBarHidden is TRUE, return YES. If FALSE, return NO.     return (self.statusBarHidden) ? YES : NO; } 

And then, in your button click method:

- (void) buttonClick:(id)sender {     // Switch BOOL value     self.statusBarHidden = (self.statusBarHidden) ? NO : YES;      // Update the status bar     [UIView animateWithDuration:0.25 animations:^{         [self setNeedsStatusBarAppearanceUpdate];     }]; } 

To set the animation style, you use this:

-(UIStatusBarAnimation)preferredStatusBarUpdateAnimation {     return UIStatusBarAnimationSlide; } 

And to customize the style:

- (UIStatusBarStyle)preferredStatusBarStyle {     return UIStatusBarStyleLightContent; } 
like image 92
awfulcode Avatar answered Sep 18 '22 11:09

awfulcode