How to change UIViewControllerBasedStatusBarAppearance to YES/NO programmatically in iOS 7?

喜夏-厌秋 提交于 2019-12-22 08:09:20

问题


My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only green battery indicator in the corner. How can I change the status bar text color to Green or Orange like it is on the home screen?

I know about

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the plist

  2. In viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

  3. Add the following method:

    -(UIStatusBarStyle)preferredStatusBarStyle{ 
        return UIStatusBarStyleLightContent; 
    }
    

How can I change UIViewControllerBasedStatusBarAppearance programmatically?

Thanks in advance...


回答1:


As mention by others add "View controller-based status bar appearance' in your application's info.plist and set it to Type: Boolean and Value: NO

For your ready reference:

In iOS 9

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

method is deprecated.

So you can use this:

application.statusBarStyle = UIStatusBarStyleLightContent;

add this line of code in method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

which is present in appDelegate.m file. This will change the status bar text color throughout your application.

So if you have any screen which has background may be dark or light then in that screen you can the status bar color by making use of:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;

Hope this helps.




回答2:


In Info.plist set 'View controller-based status bar appearance' as NO.

then,add this in your appdelegate.m class in didfinishlaunchingwithoptions method.

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

this works for ios 7.



来源:https://stackoverflow.com/questions/21424341/how-to-change-uiviewcontrollerbasedstatusbarappearance-to-yes-no-programmaticall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!