Knowledge about status bar settings (Uistatusbar) on iOS
The status bar on iOS means the top 20-pixel high section.
The status bar is divided into two parts, to distinguish between the two concepts, the following will be used:
foreground part : Refers to the display of battery, time and other parts;
background : The background part of the display is black or picture;
such as: The foreground part is white, the background part is black
注意:这里只涉及到ios7以及更高版本,低版本下面的讲解可能无效。
Set the foreground portion of the StatusBar
Simply put, the color that displays the battery level, the time, the network part,
Only two colors can be set here:
- Default Black (uistatusbarstyledefault)
- White (uistatusbarstylelightcontent)
There are two places that can be set: plist settings inside and in the program code
1.plist Setup StatusBar
Add a line of Uistatusbarstyle(or "Status bar style") to the plist, where you can set two values, which is the above mentioned two
uistatusbarstyledefault and uistatusbarstylelightcontent
This way, when the app launches the launch page, the StatusBar style is the plist set above.
2. Set StatusBar in the program code
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Or
//相对于上面的接口,这个接口可以动画的改变statusBar的前景色 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
Not only that, iOS is also very intimate in the Uiviewcontroller also added several interfaces,
The goal is to have the status bar customize the foreground portion of the StatusBar based on the currently displayed Uiviewcontroller.
-(Uistatusbarstyle) Preferredstatusbarstyle; //return directly to the status bar style of your current interface
-(Uiviewcontroller *) Childviewcontrollerforstatusbarstyle;
-(void) setneedsstatusbarappearanceupdate
-(Uistatusbarstyle) Preferredstatusbarstyle:
Rewrite this method in your own Uiviewcontroller and return the value you need (Uistatusbarstyledefault or uistatusbarstylelightcontent);
Attention:
- Here if you simply return a fixed value, then the Uiviewcontroller display, the program will call the method immediately, to change the foreground part of StatusBar;
- If the Uiviewcontroller is already in the display at the moment, you may also want to change the foreground color of the statusbar in the current page, then you first need to call the following setneedsstatusbarappearanceupdate method (This method notifies the system to invoke the current Uiviewcontroller Preferredstatusbarstyle method), this and UIView setneedsdisplay Principle (after calling the UIView object's setneedsdisplay method, the system will be called to redraw the view the next time the page refreshes, the system can refresh 60 times the page 1 seconds, depending on the program settings).
-(Uiviewcontroller *) Childviewcontrollerforstatusbarstyle:
This interface is also important, and the default return value is nil. When we call setneedsstatusbarappearanceupdate , the system calls Application.window's Rootviewcontroller Preferredstatusbarstyle method, our program is generally used Uinavigationcontroller to do root, if this is the case, then our own uiviewcontroller in the Preferredstatusbarstyle will not be called at all;
In this case, the Childviewcontrollerforstatusbarstyle comes in handy,
We're going to subclass a uinavigationcontroller and rewrite the childviewcontrollerforstatusbarstyle method in this subclass, as follows:
- (UIViewController *)childViewControllerForStatusBarStyle{ return self.topViewController;}
The above code means, do not call my own (that is Uinavigationcontroller) the Preferredstatusbarstyle method, but to call Navigationcontroller.topviewcontroller 's preferredstatusbarstyle method, so to write, The Preferredstatusbarstyle method of the currently displayed Uiviewcontroller can be guaranteed to affect the foreground portion of the statusbar.
In addition, sometimes our currently displayed Uiviewcontroller may have multiple childviewcontroller, overriding the current Uiviewcontroller Childviewcontrollerforstatusbarstyle method, let Childviewcontroller's Preferredstatusbarstyle take effect ( The current Uiviewcontroller Preferredstatusbarstyle will not be called).
Simply put, as long as Uiviewcontroller overrides the Childviewcontrollerforstatusbarstyle method return value is not nil, then Uiviewcontroller's The Preferredstatusbarstyle method will not be called by the system and the system will call Childviewcontrollerforstatusbarstyle The Uiviewcontroller method returns the Preferredstatusbarstyle method of the.
-(void) Setneedsstatusbarappearanceupdate:
Let the system call Application.window's Rootviewcontroller preferredstatusbarstyle method, if Rootviewcontroller's Childviewcontrollerforstatusbarstyle return value is not nil, refer to the above explanation.
Set the "Background section" of the StatusBar
The background part, simply speaking, is the background color; there are two ways to change it:
System-Provided methods
navigationbar setbartintcolor interface, this interface can change the background color of StatusBar
Note: Once you have set the Navigationbar -(void) SetBackgroundImage: (UIImage *) backgroundimage forbarmetrics: ( uibarmetrics) Barmetrics interface, then the above Setbartintcolor interface can not change the background color StatusBar, statusbar the background color will become pure black.
Another way
Create a UIView,
Set the frame.size and StatusBar size of the UIView,
Set the Frame.origin for the UIView to {0,-20},
Set the background color of the UIView to the color of the statusbar you want,
Addsubview the UIView on the Navigationbar.
State of the status bar is set in iOS