Skip to content

Instantly share code, notes, and snippets.

@Innovatewithapple
Created July 11, 2023 07:16
Show Gist options
  • Save Innovatewithapple/b1474dae333cb713e855896d2aa27c8f to your computer and use it in GitHub Desktop.
Save Innovatewithapple/b1474dae333cb713e855896d2aa27c8f to your computer and use it in GitHub Desktop.
iOS Status bar color change working with iOS13+. Call this function in viewDidLoad() method
func SetupStatusBar(color:String) {
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor(hexString: color)
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