Detailed explanation of UIViewControllerBasedStatusBarAppearance action in ios7

  • 2021-07-03 00:56:11
  • OfStack

Detailed Explanation of UIViewControllerBasedStatusBarAppearance in ios7

When adapting to iOS7, many articles will mention UIViewControllerBasedStatusBarAppearance. I don't quite understand its actual function. When I use it, I find that the actual function of UIViewControllerBasedStatusBarAppearance is as follows:

This property only affects how the font color on status bar is set to be dark (black) or bright (white), and has no effect on the background color of status bar. The background color of status bar is always transparent on iOS7.

apple official description of UIViewControllerBasedStatusBarAppearance:

UIViewControllerBasedStatusBarAppearance (Boolean - iOS) specifies whether the status bar appearance is based on the style preferred by the view controller that is currently under the status bar. When this key is not present or its value is set to YES, the view controller determines the status bar style. When the key is set to NO, view controllers (or the app) must each set the status bar style explicitly using the UIApplication object.

That is, UIViewControllerBasedStatusBarAppearance (1 Boolean value) specifies whether the appearance of the status bar is based on the preferred style assigned to the status bar by the current view controller. When this key does not exist, or its value is set to YES (that is, the default value for this key is yes), the view controller determines the style of the status bar. When the key is set to NO, the view controller (or application) must display the setting status bar style through the UIApplication object (that is, the setStatusBarStyle method of UIApplication).

It is not difficult to see from the official document of apple that there are only two ways to set the status bar style (dark or bright) on ios7. The first is to return the status bar style through the callback method preferredStatusBarStyle in controller, and the second is to set it through setStatusBarStyle of UIApplication object. UIViewControllerBasedStatusBarAppearance actually specifies whether to use the first method first (it is not difficult to understand why this key value is called UIViewControlle "based").

So when UIViewControllerBasedStatusBarAppearance does not exist in plist, key, or key exists, and value is YES,
The preferredStatusBarStyle method of viewController takes effect on the setting of status bar;

When value corresponding to UIViewControllerBasedStatusBarAppearance is NO,
[UIApplication sharedApplication] The setting of the status bar takes effect through the method setStatusBarStyle.

Hide status bar

Sometimes we need to hide the status bar, so at this time we can use the override method prefersStatusBarHidden in view controller, as shown in the following code:


- (BOOL)prefersStatusBarHidden 
{ 
  return YES; 
} 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: