NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to hide and show the status bar

Paul Hudson    @twostraws   

Updated for Xcode 14.2

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.

Hacking with Swift is sponsored by Waldo

SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.

Try for free today!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.