Status bar disappears after unlocking with SwiftUI

Viewed 306

I have an app with Status Bar initially hidden: YES and

NavigationView {
}
.statusBar(hidden: true)

When the app starts, the bar is not visible but its space is not occupied (so it looks like additional padding from top). But when I lock/unlock the phone this padding disappears thus moving the whole app closer to the top.

Any suggestions what's causing it?

1 Answers

Problem

enter image description here

Just tested it with the following view and it really jumps up after you lock/unlock the screen. Try to take a screenshot and it jumps up as well (found this accidentally when I was taking screenshots for the answer).

struct ContentView: View {
    var body: some View {
        NavigationView {
            Color.green
                .navigationBarTitle("No status bar")
        }
        .statusBar(hidden: true)
    }
}

Workaround

Just add the line marked in the code below.

struct ContentView: View {
    var body: some View {
        NavigationView {
            Color.green
                .navigationBarTitle("No status bar")
        }
        .edgesIgnoringSafeArea(.all) // <-------
        .statusBar(hidden: true)
    }
}

IMHO it should work without this line, but it doesn't apparently. This problem is here even if I set (Info.plist):

  • UIViewControllerBasedStatusBarAppearance to YES
  • UIStatusBarHidden to YES

Or if I hide status bar directly on the ContentView (SceneDelegate):

  • let contentView = ContentView().statusBar(hidden: true)
Related