Skip to main content
Updated with orientation bug fix
Source Link
FreakyCoder
  • 879
  • 1
  • 12
  • 25

After iOS 13 and Swift 5, we needI have a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

if #available(iOS 13.0, *) {
    let app = UIApplication.shared
    let statusBarHeight: CGFloat = app.statusBarFrame.size.height

    let statusbarView = UIView(frame:)
 CGRect  statusbarView.backgroundColor = UIColor.red
   view.addSubview(x:statusbarView)

 0, y: 0,statusbarView.translatesAutoresizingMaskIntoConstraints width= false
   statusbarView.heightAnchor
     .constraint(equalToConstant: UIScreenstatusBarHeight).mainisActive = true
   statusbarView.boundswidthAnchor
     .sizeconstraint(equalTo: view.widthwidthAnchor, heightmultiplier: statusBarHeight)1.0)
 .isActive = true
   statusbarView.backgroundColortopAnchor
     .constraint(equalTo: view.topAnchor).isActive = UIColortrue
   statusbarView.redcenterXAnchor
    view .addSubviewconstraint(statusbarViewequalTo: view.centerXAnchor).isActive = true

} else {
      let statusBar = UIApplication.shared.value(forKeyPath: 
   "statusBarWindow.statusBar") as? UIView
      statusBar?.backgroundColor = UIColor.red
}

Gist

Also, check the article iOS 13 How to Change StatusBar Color? or Github Example: iOS StatusBar Example

One last thing, you can still change statusbar style with :

 override var preferredStatusBarStyle : UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
    //return UIStatusBarStyle.default   // Make dark again
}

After iOS 13 and Swift 5, we need a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

if #available(iOS 13.0, *) {
    let app = UIApplication.shared
    let statusBarHeight: CGFloat = app.statusBarFrame.size.height

    let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
     statusbarView.backgroundColor = UIColor.red
    view.addSubview(statusbarView)
} else {
    let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
    statusBar?.backgroundColor = UIColor.red
}

Also, check the article iOS 13 How to Change StatusBar Color? or Github Example: iOS StatusBar Example

One last thing, you can still change statusbar style with :

override var preferredStatusBarStyle : UIStatusBarStyle {
   return UIStatusBarStyle.lightContent
   //return UIStatusBarStyle.default   // Make dark again
}

I have a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

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
}

Gist

Also, check the article iOS 13 How to Change StatusBar Color?

One last thing, you can still change statusbar style with :

 override var preferredStatusBarStyle : UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
    //return UIStatusBarStyle.default   // Make dark again
}
Source Link
FreakyCoder
  • 879
  • 1
  • 12
  • 25

After iOS 13 and Swift 5, we need a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

if #available(iOS 13.0, *) {
    let app = UIApplication.shared
    let statusBarHeight: CGFloat = app.statusBarFrame.size.height

    let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
    statusbarView.backgroundColor = UIColor.red
    view.addSubview(statusbarView)
} else {
    let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
    statusBar?.backgroundColor = UIColor.red
}

Also, check the article iOS 13 How to Change StatusBar Color? or Github Example: iOS StatusBar Example

One last thing, you can still change statusbar style with :

override var preferredStatusBarStyle : UIStatusBarStyle {
   return UIStatusBarStyle.lightContent
   //return UIStatusBarStyle.default   // Make dark again
}