10
votes

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

4
Possible duplicate of 56651245.chumps52
@chumps52 i'ts not duplicate, because i cant change only the text color..user9172697

4 Answers

12
votes

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
    }
8
votes

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)
4
votes

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.

-3
votes

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
}}