iosswiftcolorsstatusbaruistatusbar

Status bar background color does not change to custom color


When I change status bar background color to native UIColor.gray it changes. But when I want to use custom color it turn black color.

UIApplication.shared.statusBarView?.backgroundColor = UIColor.gray - this code workes correct. Status bar background color is gray

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 30/255, alpha: 1) - this code workes incorrect. Status bar background color is black


Solution

  • First of all, set View controller-based status bar appearance property No in info.plist file.

    Then add the following code in didFinishLaunchingWithOptions method of AppDelegate Class.

    extension UIApplication {
    var statusBarView: UIView? {
        if #available(iOS 13.0, *) {
            let tag = 5111
            if let statusBar = self.keyWindow?.viewWithTag(tag) {
                return statusBar
            } else {
                let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
                statusBarView.tag = tag
    
                self.keyWindow?.addSubview(statusBarView)
                return statusBarView
            }
        } else {
            if responds(to: Selector(("statusBar"))) {
                return value(forKey: "statusBar") as? UIView
            }
        }
        return nil}
    }
    

    I hope this would help you.