Updated for Xcode 12.0
We can hide and show the iOS status bar using SwiftUI’s statusBar()
modifier. This takes one hidden
parameter that must be either true or false, depending the behavior you want:
YourView()
.statusBar(hidden: true)
If you want status bar visibility to be dependent on some program state, use an @State
Boolean in place of a hard-coded value. For example, this creates a hideStatusBar
Boolean that gets toggled when a button is tapped, which in turn controls whether the status bar is showing or not:
struct ContentView: View {
@State var hideStatusBar = false
var body: some View {
Button("Toggle Status Bar") {
withAnimation {
self.hideStatusBar.toggle()
}
}
.statusBar(hidden: hideStatusBar)
}
}
As you can see, that toggles the Boolean inside a withAnimation
block, which causes the status bar to fade in and out smoothly.
SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.