In iOS7, not only does the style of the application change, but the status bar changes greatly. We can see that the status bar of UIViewController is basically integrated with the navigation bar. Therefore, the hide/show status method of UIVIEWCONTROLLER is different from that of other versions. In versions earlier than iOS 7, hide/show is implemented using the following code:
[[UIApplication sharedApplication] setStatusBarHidden:YES(NO) withAnimation:UIStatusBarAnimationSlide];
In iOS7, this method fails by default. Go to the setStatusBarHidden: withAnimation: declared header file and read the following comments: // Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system. in iOS7, the status bar is dependent on UIViewController by default, that is, the status bar varies with UIViewController. In this default method, the global method setStatusBarHidden: withAnimation: does not work.
Google finds that there are two solutions:
If you just hide the status bar, you only need to implement two new methods by default.
-(UIStatusBarStyle) preferredStatusBarStyle {return style; // UIStatusBarStyleDefault = 0 black text, use it in light background // UIStatusBarStyleLightContent = 1 white text, use it in dark background}-(BOOL) prefersStatusBarHidden {return NO; // if NO is returned, YES is returned and hiden is returned}
The preceding callback method returns the style when the status bar is displayed. The following callback controls whether the status bar is displayed.
Calling the following code will trigger the above callback.
[self setNeedsStatusBarAppearanceUpdate];
If you want to make an animation between hiden/show, use the following code:
[UIView animateWithDuration:0.5 animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
Or call the following code:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
Another method is in infor. in plist, add key: UIViewControllerBasedStatusBarAppearance and set its value to NO. This tells the system that the status bar does not depend on UIViewController. in this way, you can use the above method to obtain the hiden status bar.
After setting these settings, we will still find some problems, that is, although the status bar is missing, it is replaced by a black area, so we also need to adjust the UIViewController view. The specific code is:
-(void)viewDidLayoutSubviews{ CGRect viewBounds = self.view.bounds; CGFloat topBarOffset = 20.0; viewBounds.origin.y = -topBarOffset; self.view.bounds = viewBounds; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//for status bar style}