Search code examples
swiftviewswiftuistatusbar

View going under Status Bar in SwiftUI


How can I stop a view in SwiftUI going under the Status Bar when using a ScrollView? I already tried to place a view directly under the status bar, but it didn't have an effect.

I didn't use .edgesIgnoringSafeArea anywhere.

My Code:

ScrollView {
    HStack {
        Spacer()
        Text("ScrollMe")
            .padding()
    }
}

Screenshot:

Example


Solution

  • This is expected behavior for a ScrollView to scroll off the screen into the safe area insets.

    If you really want to avoid this behavior, you need some other interim view with a non-zero height that will sit below the safe area. For example, modifying your code:

    struct ContentView: View {
        var body: some View {
            VStack {
                Spacer().frame(height: 1)
                ScrollView {
                    HStack {
                        Spacer()
                        Text("ScrollMe")
                            .padding()
                    }
                }
            }
        }
    }