Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 254
Said-Abdulla Atkaev Avatar asked Dec 05 '25 14:12

Said-Abdulla Atkaev


1 Answers

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.

like image 142
Inder Jagdeo Avatar answered Dec 08 '25 07:12

Inder Jagdeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!