UIApplication.sharedApplication().setStatusBarStyle() deprecated in iOS 9

前端 未结 9 656
野的像风
野的像风 2021-02-01 13:34

I have been using

UIApplication.sharedApplication().setStatusBarStyle()

In my appDelegate and it has worked fine, but since iOS 9, this method

9条回答
  •  醉梦人生
    2021-02-01 14:21

    Swift 5, iOS 13.5+

    I'm gonna make a recap that I hope it's gonna be helpful.

    #1: General solution without using preferredStatusBarStyle

    To answer the question, if we don't want to care about exceptions screens and not use the preferredStatusBarStyle property from view controllers as Apple recommends, I think that indeed setting the UIViewControllerBasedStatusBarAppearance to false and changing the Status Bar Style under General settings -> Deployment info to light, as @Rick already recommended, is the way to go.

    #2: Using preferredStatusBarStyle

    For my case, I wanted to be able to have the UIStatusBarStyle.lightContent as default, but with some screens having the UIStatusBarStyle.default; and in these kind of cases, the solution #1 isn't possible.

    Since also having a general extension to UIViewController that allows to change the default value isn't obviously possible for this property, the only and best way to proceed in these cases if we don't want to use deprecated methods, is via inheritance.

    So, a possibility is to have a general BaseViewController (and also the BaseNavigationController if you use one) that you controller inherits from, that sets the preferredStatusBarStyle to .lightContent.

    With this approach, now you can simply set the style to default where needed, while maintaining the lightContent as default.

提交回复
热议问题