10

For ios 13 I can't set text color of the status bar. How I can get the view of statusBarManager? How I can change the text color only?

due to:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

My current code:

    func setStatusBarTextColor(_ color: UIColor) {
        if #available(iOS 13.0, *) {
            // How to do for iOS 13??
        } else {
            if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
                statusBar.setValue(color, forKey: "foregroundColor")
            }
        }
    }

I have already found this https://stackoverflow.com/a/57394751/9172697 but it's not what i looking for

5 Answers5

12

IOS 13.0 and XCode 11.0 with Swift 5.0 100% Working

    if #available(iOS 13.0, *) {


       let statusBar1 =  UIView()
       statusBar1.frame = UIApplication.shared.keyWindow?.windowScene?.statusBarManager!.statusBarFrame as! CGRect
       statusBar1.backgroundColor = UIColor.black

       UIApplication.shared.keyWindow?.addSubview(statusBar1)

    } else {

       let statusBar1: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
       statusBar1.backgroundColor = UIColor.black
    }
Maulik Patel
  • 2,045
  • 17
  • 24
8

You can use like this for iOS 13 :

   let statusBar =  UIView()
   statusBar.frame = UIApplication.shared.statusBarFrame
   statusBar.backgroundColor = UIColor.red
   UIApplication.shared.keyWindow?.addSubview(statusBar)
Pradeep Singh
  • 150
  • 2
  • 5
4

The color of text in the status bar was never up to you; what you were doing was always wrong. Use your top level view controller to override preferredStatusBarStyle. You have two choices, .lightContent and .darkContent, and you should use neither because you want to support light/dark mode.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

IOS 15, Xcode 13.2.1, Swift 5

I was able to get this to work without errors or warnings using the following:

func statusBarColor() {
    if #available(iOS 13.0, *) {
        
        let statusBar2 =  UIView()
        if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
            statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
            statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
            UIApplication.shared.windows.first?.addSubview(statusBar2)
        }
    } else {
        let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
    }
}

Use: Call the function in viewDidLoad for single scene applications or applications with only a single statusBar color change needed. For applications with multiple scenes calling for different statusBar colors I recommend calling the function in viewWillAppear.

Michael
  • 1
  • 3
-3

UPDATED ANSWER

To change the text color on status bar you only need to set the style. (you don't have to many options, text color in status bar can be white or black)

If you wan to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. override preferredStatusBarStyle in your view controller.

If you want to change the status bar background for iOS 13 use this code:

extension UIApplication {

class var statusBarBackgroundColor: UIColor? {
    get {
        return statusBarUIView?.backgroundColor
    } set {
        statusBarUIView?.backgroundColor = newValue
    }
}

class var statusBarUIView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 987654321

        if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(tag) {
            return statusBar
        }
        else {
            let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
            statusBarView.tag = tag

            UIApplication.shared.keyWindow?.addSubview(statusBarView)
            return statusBarView
        }
    } else {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil
}}
Sattar
  • 393
  • 5
  • 18