Knowledge popularization
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 section : is the background part of the display black or picture;
For example: 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
//Relative to the above interface, this interface can be animated to change the foreground color of the 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;
-
-(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 is displayed, The program will call this method immediately, to change the foreground part of the StatusBar,
- Method (This method notifies the system to invoke the current Uiviewcontroller preferredstatusbarstyle method), this and UIView setneedsdisplay the same principle (call the UIView object's setneedsdisplay method, the system will be the next page refresh, the call to redraw the view, the system can refresh 60 times the fastest 1 seconds page, depending on the program settings.
-(Uiviewcontroller *) Childviewcontrollerforstatusbarstyle:
This interface is also important, the default return value is nil. When we call setneedsstatusbarappearanceupdate , The system will call Application.window's Rootviewcontroller preferredstatusbarstyle method, our program is generally used Uinavigationcontroller root, if this is the case, then our own uiviewcontroller in the preferredstatusbarstyle is not called at all;
in this case Childviewcontrollerforstatusbarstyle comes in handy,
method, as follows:
-(uiviewcontroller *) childviewcontrollerforstatusbarstyle{return Self ;}
The above code means, do not call me (that is Uinavigationcontroller) preferredstatusbarstyle method, instead of calling ; margin:0px ">preferredstatusbarstyle method, so write, you can ensure that the current display of Uiviewcontroller preferredstatusbarstyle method can affect the foreground portion of StatusBar.
In addition, sometimes our currently displayed Uiviewcontroller may have multiple childviewcontroller, overriding the current Uiviewcontroller childviewcontrollerforstatusbarstyle method, let Childviewcontroller preferredstatusbarstyle is in effect ( 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.
Refer
Information Property List Key Reference:ios Keys
New features that are easily overlooked in iOS7-Cocoachina Apple Development Chinese station
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Settings related to IOS status bar (Uistatusbar)