Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active June 15, 2022 08:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save WrathChaos/5c4498e4ebfc19ca1de8983a485d587e to your computer and use it in GitHub Desktop.
Save WrathChaos/5c4498e4ebfc19ca1de8983a485d587e to your computer and use it in GitHub Desktop.
iOS 13 and Below How to change Status Bar Color?
if #available(iOS 13.0, *) {
    let app = UIApplication.shared
    let statusBarHeight: CGFloat = app.statusBarFrame.size.height
    
    let statusbarView = UIView()
    statusbarView.backgroundColor = UIColor.red
    view.addSubview(statusbarView)
  
    statusbarView.translatesAutoresizingMaskIntoConstraints = false
    statusbarView.heightAnchor
        .constraint(equalToConstant: statusBarHeight).isActive = true
    statusbarView.widthAnchor
        .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
    statusbarView.topAnchor
        .constraint(equalTo: view.topAnchor).isActive = true
    statusbarView.centerXAnchor
        .constraint(equalTo: view.centerXAnchor).isActive = true
  
} else {
    let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
    statusBar?.backgroundColor = UIColor.red
}
@abhi21git
Copy link

It would be helpful if you make a function which adds or remove the background view in status bar based on a flag value.

@WrathChaos
Copy link
Author

It would be helpful if you make a function which adds or remove the background view in status bar based on a flag value.

I can update it if you wanna contribute it :) In general, people just need these other things like your advice would be so specific.

@sugitatestblue
Copy link

'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.

@sugitatestblue
Copy link

var statusBarTagNumber = 1
if #available(iOS 13.0, *) {
    let app = UIApplication.shared
    let statusBarHeight: CGFloat = app.statusBarFrame.size.height
    
    for subview in view.subviews {
        if subview.tag == statusBarTagNumber {
            subview.backgroundColor = backgroundColor
            return
        }
    }

    let statusbarView = UIView()
    statusbarView.tag = statusBarTagNumber
    statusbarView.backgroundColor = UIColor.red
    view.addSubview(statusbarView)
  
    statusbarView.translatesAutoresizingMaskIntoConstraints = false
    statusbarView.heightAnchor
        .constraint(equalToConstant: statusBarHeight).isActive = true
    statusbarView.widthAnchor
        .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
    statusbarView.topAnchor
        .constraint(equalTo: view.topAnchor).isActive = true
    statusbarView.centerXAnchor
        .constraint(equalTo: view.centerXAnchor).isActive = true
  
} else {
    let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
    statusBar?.backgroundColor = UIColor.red
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment