There are a few different ways you can hide the status bar in a SwiftUI project depending on your intentions. I’ve listed a few in no particular order. All the examples work with iOS 13 & iOS 14 using Xcode 11 & Xcode 12 with the exception of OPTION-2.
OPTION-1
In your info.plist file make sure you have these settings:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Add the following UIViewController extension
extension UIViewController {
func prefersStatusBarHidden() -> Bool {
return true
}
}
OPTION-2 (iOS 14 & Xcode 12 Only)
In your info.plist file make sure you have these settings:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Add an AppDelegate to a SwiftUI app as described in this link but add the the line noted below.
https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UIApplication.shared.isStatusBarHidden = true // <== ADD THIS LINE
return true
}
}
Note you’ll get a deprecated call warning, but it should still work.
OPTION-3
In your info.plist file make sure you have these settings:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Make sure your initial SwiftUI View is a Navigation view where you hide the status bar. Then if you navigate to a tab bar view or any subsequent views the status bar will be hidden.
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: MyTabView()) {
Text("Go To Tabview")
}
}
.edgesIgnoringSafeArea(.all)
.statusBar(hidden: true)
}
}
struct MyTabView: View {
var body: some View {
TabView {
TabView1().tabItem {
Text("Tab 1")
}
TabView2().tabItem {
Text("Tab 2")
}
}
}
}
struct TabView1: View {
var body: some View {
Text("Tab View 1")
}
}
struct TabView2: View {
var body: some View {
Text("Tab View 2")
}
}
OPTION-4
Use UIStatusBarManager
https://developer.apple.com/documentation/uikit/uistatusbarmanager