2

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

Krunal
  • 77,632
  • 48
  • 245
  • 261
Said-Abdulla Atkaev
  • 4,193
  • 1
  • 11
  • 17

3 Answers3

8

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.

Inder Jagdeo
  • 229
  • 2
  • 9
  • 1
    I have no idea why, but this was the only solution that worked for me of all solutions over the internet... Why such simple thing like changing status bar colour must be so complicated? Apple, are you serious? – undefinedman May 05 '18 at 22:24
5

May this help you in Swift 4. Looks like a hacky trick but works.

You can set background color for status bar during application launch or during viewDidLoad of your view controller.

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



Here is result:

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
0

The code isnt incorrect, just that the color you are providing is dark gray itself. (R:30,G:30,B:30) represents dark gray and thats what is being used in the status bar, change this color to some other color for example (R:202,G:0,B:42) and it will display a red color.

Try

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 202/255, green: 0/255, blue: 42/255, alpha: 1)

Ahsan Ebrahim Khatri
  • 1,807
  • 1
  • 20
  • 27