IOS 7 hides the status Bar

Source: Internet
Author: User

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}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.