IOS status bar

Hide status bar

There are two ways to hide the status bar:
method: through the code control

@interface UIApplication (UIApplicationDeprecated) Setting statusBarHidden does nothing if your / application is using the default UIViewController-based status bar system. @property (readwrite, nonatomic, getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS (2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") - __TVOS_PROHIBITED; (void) setStatusBarHidden: (BOOL) hidden animated: (BOOL) animated (NS_DEPRECATED_IOS 2_0, 3_2 __TVOS_PROHIBITED); / / use -setStatusBarHidden:withAnimation: - (void) setStatusBarHidden: (BOOL) hidden withAnimation: (UIStatusBarAnimation) animation NS_DEPRECATED_IOS (3_2, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;

Note: let’s look at the Setting statusBarHidden does nothing if your / application is using the default UIViewController-based status bar system. the comments suggest that Apple developers tips if you are using a system based on the style of the status bar you these settings is not in force, in the next to be introduced by Info.plist to hide the status bar also pay attention to this what.
add a View controller-based status bar appearance settings in Info.plist, the method is set to NO so you can use the above
IOS status bar
View controller-based status note: add bar appearance is Bool type, the default is Yes

Unfortunately, after Apple iOS9 has not recommended the use of these methods, these methods can be used but will warn.

Then these methods are disabled, how to operate it? This is a new method of Apple added in UIViewController in iOS7 after Use -[UIViewController prefersStatusBarHidden] have indicated the notes in the status bar, the purpose of doing so allows developers more flexibility to customize each ViewController.

- (BOOL) prefersStatusBarHidden{return YES;}

IOS7 after the UIViewController not only provides the status bar on the setting of the function, there are other, detailed in the following.

Method two: through the Info.plist control
1, first of all, we still have to set this (the second step in the way to set the parameters of the
)

IOS status bar

2, then set (in two ways)

IOS status bar

or

IOS status bar

the two are equivalent! And the state of both is synchronous.

Status bar style

First look at what styles (see note)

Typedef NS_ENUM (NSInteger, UIStatusBarStyle) {/ / default style, black and transparent status bar, suitable for the background color is bright page UIStatusBarStyleDefault = 0 / / Dark content, for use, on light backgrounds / / white transparent status bar, suitable for the back page of the UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS view of the dark (7_0) = 1, Light / content for, use on dark backgrounds / / iOS7.0 before black white, iOS7 UIStatusBarStyleLightContent UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS with the same effect (2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1 / / iOS7.0 before the start page is gray and white, with UIStatusBarStyleLightContent UIStatusBarStyleBlackOpaque iOS7 after the same effect NS_ENUM_DEPRECATED_IOS (2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,} __TVOS_PROHIBITED;

How to set the status bar style

Setting the statusBarStyle does nothing if / your application is using the default UIViewController-based status bar system. @property (readwrite, nonatomic) UIStatusBarStyle statusBarStyle NS_DEPRECATED_IOS (2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") - __TVOS_PROHIBITED; (void) setStatusBarStyle: (UIStatusBarStyle) statusBarStyle animated: (BOOL) animated NS_DEPRECATED_IOS (2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]" __TVOS_PROHIBITED);

Also after iOS9 these methods are disabled, apple recommended in specific viewController Use -[UIViewController preferredStatusBarStyle]

- (UIStatusBarStyle) preferredStatusBarStyle{return UIStatusBarStyleLightContent;}

Note: we usually use the viewController is nested in UINavigationController, the
used in viewController – (UIStatusBarStyle) preferredStatusBarStyle; function settings and will find no effect.
here’s an article about this blog: UINavigationController and View Controller-based status bar style

In addition to the above methods in the blog, the system also provides us with a function – (UIViewController *) childViewControllerForStatusBarStyle, you can solve this problem, the following will be.

background color

IOS7 the default status bar background transparent, one way is to write our own UIView as a background to add to the state bar, so you can set the status bar color.

Another way is to change the status bar color by setting the setBarTintColor color of the navigationBar

Other functions of the status bar in UIViewController

  • PreferredStatusBarUpdateAnimation function
- (UIStatusBarAnimation) preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS __TVOS_PROHIBITED (7_0); / / Defaults to UIStatusBarAnimationFade

The function is used to set the status bar display, the disappearance of the type of animation, animation type:

Typedef NS_ENUM (NSInteger, UIStatusBarAnimation) {UIStatusBarAnimationNone, UIStatusBarAnimationFade NS_ENUM_AVAILABLE_IOS (3_2), UIStatusBarAnimationSlide NS_ENUM_AVAILABLE_IOS (3_2), __TVOS_PROHIBITED};

The default is UIStatusBarAnimationFade, when the status bar is hidden, the display state changes, the return value of the function will play a role.

  • ChildViewControllerForStatusBarStyle function
Override to return a child view / controller or nil. If non-nil, that view controller's status bar appearance attributes will be used. If nil, self is used. Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes should be called. (nullable * UIViewController) - childViewControllerForStatusBarStyle NS_AVAILABLE_IOS (7_0) __TVOS_PROHIBITED;

This function returns the default return nil, the system will call the preferredStatusBarStyle function for the current viewControllerA; if the return value is another viewControllerB, then the system will call the viewControllerB preferredStatusBarStyle function.

This function can be used to solve the nested UINavigationController settings invalid style.

To explain why the preferredStatusBarStyle function nested UINavigationController viewController settings:
is invalid in our nested UINavigationController, our
AppDelegate.window.rootViewController
is usually we create navigationController, then will first call the childViewControllerForStatusBarStyle navigationController function, because the default returns nil, then navigationController is called the preferredStatusBarStyle function itself, so we through the preferredStatusBarStyle function to set the style of the status bar in the viewController will not be invoked, so it is invalid.

So we have to create a
NavigationController, which is inherited from UINavigationcontroller, and rewrite the
childViewControllerForStatusBarStyle function in this subclass

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

The childViewControllerForStatusBarStyle function in this navigationController will return navigationController in the top viewController, then the preferredStatusBarStyle function viewController in the setting of the system will be informed

  • ChildViewControllerForStatusBarHidden function
(nullable UIViewController *) childViewControllerForStatusBarHidden NS_AVAILABLE_IOS (7_0) __TVOS_PROHIBITED;

ChildViewControllerForStatusBarHidden function using the same principle, no longer repeat.

  • PreferredStatusBarUpdateAnimation function
Override to return the type of / animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden. (UIStatusBarAnimation) preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS __TVOS_PROHIBITED (7_0); / / Defaults to UIStatusBarAnimationFade

The animation is as follows

Typedef NS_ENUM (NSInteger, UIStatusBarAnimation) {UIStatusBarAnimationNone, UIStatusBarAnimationFade NS_ENUM_AVAILABLE_IOS (3_2), UIStatusBarAnimationSlide NS_ENUM_AVAILABLE_IOS (3_2), __TVOS_PROHIBITED};

This function returns the animation effect. Only when the animation display only the return value of prefersStatusBarHidden changes in
function, at the same time by calling the
[self setNeedsStatusBarAppearanceUpdate] function to redraw the status bar

application

We can hide the system status bar, and then customize the UIWindow
by setting the setWindowLevel:UIWindowLevelStatusBar to customize the status bar.

Copyright statement: from MajorLMJ technology blog original works, reproduced must indicate the source and the corresponding link!