Change the iOS status bar color

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.