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

2
  • Possible duplicate of 56651245. Commented Sep 15, 2019 at 11:42
  • @chumps52 i'ts not duplicate, because i cant change only the text color.. Commented Sep 15, 2019 at 11:47

5 Answers 5

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
    }
Sign up to request clarification or add additional context in comments.

1 Comment

keyWindow is deprecated in iOS 13.
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)

Comments

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.

Comments

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.

Comments

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

3 Comments

He does not want to change the background, but the text of the statusbar
yeah, but read the code he wrote, it's about background. I will update my answer and i will add the answer also for the text color
keyWindow and statusBarFrame are deprecated in iOS 13.