Settings of the iOS Status Bar (UIStatusBar)

Source: Internet
Author: User

Settings of the iOS Status Bar (UIStatusBar)
Knowledge popularization

The status bar on iOS refers to the top 20 pixels high.
The status bar consists of two parts:

  • Foreground: displays the battery and time;

  • Background: displays the black or image background;

    For example, the foreground is white and the background is black.

    Note: Only ios7 and later versions are involved. The description below may be invalid.

    Set foreground in statusBar]

    Simply put, it is to set the color that shows the battery power, time, and network,
    Only two colors can be set here:

    • Default black (UIStatusBarStyleDefault)
    • White (UIStatusBarStyleLightContent)

      You can set two options: plist setting and program code.

      1. plist set statusBar

      Add a UIStatusBarStyle line in plist (or "Status bar style"). Here we can set two values, that is, the two values mentioned above.
      UIStatusBarStyleDefault and UIStatusBarStyleLightContent

      In this way, when the launch page of the app is displayed, the statusBar style is the style set by plist above.

      2. Set statusBar in the program code
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];  

      Or

      // Compared with the above interface, this interface can be used to animated change the foreground color of statusBar [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent animated: YES];

      In addition, ios also adds several interfaces in UIViewController,
      The purpose is to allow the status bar to customize the foreground part of statusBar Based on the UIViewController currently displayed.

      • -(UIStatusBarStyle) preferredStatusBarStyle;

      • -(UIViewController *) childViewControllerForStatusBarStyle;

      • -(Void) setNeedsStatusBarAppearanceUpdate

        -(UIStatusBarStyle) preferredStatusBarStyle:

        Override this method in your own UIViewController and return the value you need (UIStatusBarStyleDefault or UIStatusBarStyleLightContent );

        Note:

        • Here, if you only return a fixed value, the program will immediately call this method when the UIViewController is displayed to change the foreground part of statusBar;
        • If the UIViewController is currently displayed, you may need to change the foreground color of statusBar from time to time on the current page, first, you need to call the following setNeedsStatusBarAppearanceUpdate method (this method will notify the system to call the preferredStatusBarStyle method of the current UIViewController). This is similar to the setNeedsDisplay principle of UIView (call the setneedsdis, when the system refresh the page next time, it calls to re-paint the view. The system can refresh the page 60 times in one second, depending on the program settings ).

          -(UIViewController *) childViewControllerForStatusBarStyle:

          This interface is also very important. The default return value is nil. When setNeedsStatusBarAppearanceUpdate is called, The system calls application. the preferredStatusBarStyle method of rootViewController in window. In our program, UINavigationController is generally used as the root. In this case, the preferredStatusBarStyle in our UIViewController will not be called at all;
          In this case, childViewControllerForStatusBarStyle comes in handy,
          We want to subclass A UINavigationController. In this subclass, override the childViewControllerForStatusBarStyle method as follows:

          - (UIViewController *)childViewControllerForStatusBarStyle{    return self.topViewController;}

          The above code means not to call the preferredStatusBarStyle method of myself (that is, UINavigationController), but to call navigationController. the preferredStatusBarStyle method of topViewController. In this way, the preferredStatusBarStyle method of UIViewController currently displayed can affect the foreground part of statusBar.

          In addition, sometimes our currently displayed UIViewController may have multiple childviewcontrollers, which overwrite the current callback method of UIViewController to bring the preferredStatusBarStyle of childViewController into effect (the preferredStatusBarStyle of UIViewController will not be called ).

          To put it simply, as long as the callback method returned by UIViewController is not nil, The preferredStatusBarStyle method of UIViewController will not be called by the system. The system will call the preferredStatusBarStyle method returned by childViewControllerForStatusBarStyle.

          -(Void) setNeedsStatusBarAppearanceUpdate:

          The system calls the preferredStatusBarStyle method of rootViewController of application. window. If the return value of childViewControllerForStatusBarStyle of rootViewController is not nil, refer to the above description.

          Set the background of statusBar]

          The background is the background color. There are two ways to change the background color:

          Methods provided by the system

          SetBarTintColor interface of navigationBar. This interface can be used to change the background color of statusBar.

          Note: Once the navigationBar-(void) setBackgroundImage :( UIImage *) backgroundImage forBarMetrics :( UIBarMetrics) barMetrics interface is set, the above setBarTintColor interface cannot change the background color of statusBar, the background color of statusBar is black.

          A different path

          Create a UIView,
          Set the frame. size of the UIView to the same size as the statusBar,
          Set frame. origin of the UIView to {0,-20 },
          Set the background color of the UIView to the color of your desired statusBar,
          On the navigationBar, addSubView the UIView.

          Refer:

          Information Property List Key Reference: iOS Keys

          New features that are easily overlooked in iOS7-CocoaChina Chinese site developed by Apple

           

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.