How to hide and show the status bar
About 2 min
How to hide and show the status bar 관련
SwiftUI by Example
Back to Home
How to hide and show the status bar | SwiftUI by Example
How to hide and show the status bar
Updated for Xcode 15
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:
Text("No status bar, please")
.statusBar(hidden: true)
Important
This modifier is available only on iOS.
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 private var hideStatusBar = false
var body: some View {
Button("Toggle Status Bar") {
withAnimation {
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.
Similar solutions…
SwiftUI tips and tricks | SwiftUI by Example
SwiftUI tips and tricks
All SwiftUI property wrappers explained and compared | SwiftUI by Example
All SwiftUI property wrappers explained and compared
How to hide the tab bar, navigation bar, or other toolbars | SwiftUI by Example
How to hide the tab bar, navigation bar, or other toolbars
How to hide and show the sidebar programmatically | SwiftUI by Example
How to hide and show the sidebar programmatically
How to hide and reveal content using DisclosureGroup | SwiftUI by Example
How to hide and reveal content using DisclosureGroup