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

前端 未结 9 913
野的像风
野的像风 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:15

    To dynamically update UIStatusBarStyle on view controllers use this method

    this will also remove deprecated warning

    'setStatusBarStyle:' is deprecated: first deprecated in iOS 9.0 - Use -[UIViewController preferredStatusBarStyle]

    for calling

    [[UIApplication sharedApplication] setStatusBarStyle:style];
    

    Let's Get Started

    Objective - C

    define UtilityFunction

    +(void)setStatusBarStyle:(UIStatusBarStyle )style {    
        [[NSUserDefaults standardUserDefaults] setInteger:style forKey:@"UIStatusBarStyle"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    

    over-ride this method in your BaseViewController

    - (UIStatusBarStyle)preferredStatusBarStyle {
        UIStatusBarStyle style = [[NSUserDefaults standardUserDefaults] integerForKey:@"UIStatusBarStyle"];
        return style;
    }
    

    set UIStatusBarStyle value for the AnyViewController using a UtilityFunction like below:

    [UtilityFunctions setStatusBarStyle:UIStatusBarStyleDefault];
    
    // call below code for preferred style
    [self preferredStatusBarStyle];
    

    Swift 4.0

    define UtilityFunction

    class func setPreferedStyle(style:UIStatusBarStyle)->Void {
        UserDefaults.standard.set(style, forKey: "UIStatusBarStyle")
        UserDefaults.standard.synchronize()
    }
    

    over-ride this method in your BaseViewController

    override var preferredStatusBarStyle: UIStatusBarStyle {
        if let style: UIStatusBarStyle = UIStatusBarStyle(rawValue:UserDefaults.standard.integer(forKey: "UIStatusBarStyle")) {
            return style
        }
        return UIStatusBarStyle.lightContent
    }
    

    set UIStatusBarStyle value for the AnyViewController using a UtilityFunction like below:

    Utility.setPreferedStyle(style: .lightContent)
    
    // call below code for preferred style
    preferredStatusBarStyle()
    

提交回复
热议问题