Change the status bar color
The default color of the iOS status bar is black, but sometimes we need to change the color of the status bar for the sake of demand, usually in the following ways:
Method One:
1. Add view controller-based status bar appearance to No in info.plist;
2. Perform [[UIApplication sharedapplication] setstatusbarstyle:uistatusbarstylelightcontent] on the page.
However, this practice before ios9.0 is completely no problem, after entering the iOS9.0, there will be the following warning:
<error>: cgcontextsavegstate:invalid context 0x0. If you want to see the backtrace, please set cg_context_show_backtrace en
In order to eliminate this warning: Modify the method as follows:
1. Delete This article in Info.plist, or set the view controller-based status bar appearance to Yes
2. Increase the number of pages that need to change color: (non-global, need to add in each required Viewcontroller)
-(Uistatusbarstyle) preferredstatusbarstyle { return uistatusbarstylelightcontent;
But again, if we initialize a uinavigationcontroller or uitabbarcontroller as Rootviewcontroller, What if the appeal solution set in the sub-Viewcontroller is invalidated?
to extend a category for Uinavigationcontroller, implement the following method in the class, and call the method through Uinavigationcontroller, Then in the Viewcontroller can realize the change of the status bar color .
-(Uiviewcontroller *) childviewcontrollerforstatusbarstyle{ return Self.topviewcontroller;}
This method needs to be called when the status bar has changed:
[Self setneedsstatusbarappearanceupdate];
Set the animation effect for the status bar:
-(uistatusbaranimation) preferredstatusbarupdateanimation{ // return Uistatusbaranimationfade; return uistatusbaranimationslide;}
Hide the status bar:
If you do not set Uinavigationcontroller and so on, there are two ways to hide the status bar
Method One:
1. Add the Info.plist view controller-based status bar appearance to No;
2. Add the Info.plist status bar is initially hidden to YES.
Method Two: (Non-global)
-(BOOL) prefersstatusbarhidden{ return YES;
If you initialize a uinavigationcontroller or uitabbarcontroller as Rootviewcontroller, then the second method fails, and the solution changes the status bar color.
How to hide the status bar in one interface:
1. Add view controller-based status bar appearance to No in info.plist;
2. On the page that needs to be hidden:
When the page will appear:
-(void) viewwillappear: (BOOL) animated{ [[uiapplication sharedapplication] Setstatusbarhidden:yes Withanimation:uistatusbaranimationfade]; }
Where the page disappears:
-(void) viewwilldisappear: (BOOL) animated{ [[UIApplication sharedapplication] Setstatusbarhidden: No Withanimation:uistatusbaranimationfade];}
Change the iOS status bar color