On way to achieve this is to create a UIView with the proper position and dimension, then add it to the UIWindow directly :
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var statusBarColorView: UIView?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window?.makeKeyAndVisible()
let v = UIView(frame: CGRect(x: 0, y: 0, width: window?.frame.size.width ?? 1024, height: 20))
v.backgroundColor = UIColor.white
v.translatesAutoresizingMaskIntoConstraints = false
window?.addSubview(v)
window?.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "V:|-0-[v(==20)]",
options: [],
metrics: nil,
views: ["v": v]))
window?.addConstraints(NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[v]-0-|",
options: [],
metrics: nil,
views: ["v": v]))
return true
}
The problem with that approach is that you'll need to hide / show it according to the device orientation / currently displayed view controller yourself.
Changing color will look like this :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.statusBarColorView?.backgroundColor = UIColor.blue
}
}
You'll surely want to customize UIStatusBarStyle according to your color, you can achieve this by selecting YES
for the key UIViewControllerBasedStatusBarAppearance
in your Info.plist