2

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

J--
  • 172
  • 1
  • 14

2 Answers2

4

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()
                }
            }
        }
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94
-2

You will have to add constraints to layout the view respectively for each device. You can do it like this image, then it will have the space(f.e. 240) to the view port you selected:

yourView.heightAnchor.constraint(equalToConstant: 240)
  • Using constraints does not mean that you have to set them specifically for each device (see https://stackoverflow.com/questions/46317061/how-do-i-use-safe-area-layout-programmatically). Also, the OP specifically asked about SwiftUI, which does not use constraints like this. – jnpdx Mar 24 '21 at 15:44