1

There are so many different solutions for hiding the status bar for the whole application in SwiftUI.

Example 1:

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}

Example 2:

NavigationView {
}
.statusBar(hidden: true)

Example 4:

<key>UIStatusBarHidden</key>
<true/>

Example 5:

Status Bar Style: Hide status bar (in Target Settings)

What are they, pros and cons, and which one is the preferred one?

vasily
  • 2,850
  • 1
  • 24
  • 40

3 Answers3

1

Example 2 is the SwiftUI equivalent of what you can do in UIKit (example 1). The other examples have nothing to do with SwiftUI, but can still be the right solution if you always want it to be hidden. In other cases, where it is hidden in specific cases, I would use option 2.

Guido Hendriks
  • 5,706
  • 3
  • 27
  • 37
1

Examples 5 and 4 are the same. If you change the Status Bar Style, the Info.plist property is also changed and vice versa. This method is preferred if you want to hide the status bar for the entire application. In case you want to hide it for particular view, you need to use the second approach. The first example does exactly the same thing as the 4th example, but in my opinion the key in the Info.plist file looks more declarative.

1

Example 1

Each controller is able to hide / show the bar individually. However, if you write a generic extension for all view controllers, this basically means you are switching it off for all views. This is actually not different from switching it off entirely for the whole app via Info.plist

// switch off statusbar for the entire app (all views)
extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}

// switch off statusbar for only specific views

class MyViewController: UIViewController {
..
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

Example 2

You need to know if your ViewController is included in a container (such as UINavigationController) in that case the NavigationController takes control of the StatusBar. You might to write a solution where the navigation controller always gives the control to the topviewcontroller in this case: see iphoneX not call prefersStatusBarHidden

NavigationView {
}
.statusBar(hidden: true)

Example 4

You can set the status also once for the whole app. That is done in the Info.plist file

<key>UIStatusBarHidden</key>
<true/>

Example 5

You can setup the setting for the whole app but have it differently for each target. Thats done here.

Status Bar Style: Hide status bar (in Target Settings)
hhamm
  • 1,511
  • 15
  • 22