skip to Main Content

how am I supposed to change this piece of code?
The current solutions for this problem either cover older iOS versions
or are unanswered.
This is the line:

UIApplication.shared.statusBarStyle = .lightContent

And I get those warnings:

'statusBarStyle' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.
'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

The code works, but would be nice to do not have those warnings popping up

3

Answers


  1. You can override this property in your view controller :

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    Plus you can get the value of statusBarStyle like this:

        let style = SceneDelegate.shared?.window?.windowScene?.statusBarManager?.statusBarStyle
    

    but this is only get only property. In order to change the status bar style you can use the above code by overriding the ViewController property.

    In case you want to change the status bar style of whole app then you can use info.plist like this:
    Add "UIViewControllerBasedStatusBarAppearance" key and set its value to "false"

    And then add "UIStatusBarStyleLightContent" key and set its value to "UIStatusBarStyle".

    Login or Signup to reply.
  2. You can create a hosting controller by inheriting UIHostingController and then change the statusBarStyle like this:

    class HostingController<ContentView>: UIHostingController<ContentView> where ContentView: View {
    
      override var preferredStatusBarStyle: UIStatusBarStyle {
          return .lightContent
      }
    }
    

    And then you can set this hosting controller as your window root view controller in Scene Delegate like this:

    func scene(_ scene: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) {
    
        let contentView = YOUR_VIEW
    
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = HostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    

    I hope this will work for you and as of now I believe the only way is using HostingController.

    Login or Signup to reply.
  3. if you want to remove the warning you can use:

    if #available(iOS 13, *)
    

    This function works for me:

    func setStatusBar() {
            if #available(iOS 13, *)
            {
                let keyWindow = UIApplication.shared.connectedScenes
                        .filter({$0.activationState == .foregroundActive})
                        .compactMap({$0 as? UIWindowScene})
                        .first?.windows
                        .filter({$0.isKeyWindow}).first
                let statusBar = UIView(frame: (keyWindow?.windowScene?.statusBarManager?.statusBarFrame) ?? CGRect(x: 0, y: 0, width: screenWidth, height: statubarHeight))
                statusBar.backgroundColor = .white
                keyWindow?.addSubview(statusBar)
            } else {
                let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
                if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
                    statusBar.backgroundColor = .white
                }
                UIApplication.shared.statusBarStyle = .lightContent
            }
        }
    

    and you can use for all pages of app in "General" settings:

    enter image description here

    and in "info.plist"

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search