Proper way to hide status bar on iOS, with animation and resizing root view
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;}
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);}];
You can present and then dismiss modal view controller to hide status bar correctly
- (void)toggleStatusBar { BOOL isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden]; [[UIApplication sharedApplication] setStatusBarHidden:!isStatusBarHidden]; UIViewController *vc = [[UIViewController alloc] init]; [self presentViewController:vc animated:NO completion:nil]; [self dismissViewControllerAnimated:NO completion:nil]; [vc release];}
I used this code in the "willAnimateRotationToInterfaceOrientation" method for landscape orientation and everything is working correctly. But I don't know if it will work with animation.