4

I'd like to remove bottom padding, the white space between the red space. Is there any way to achieve it?

enter image description here

Test Code:

struct ContentView: View {
    var body: some View {
        return NavigationView {
            VStack {
                // the same result with using List instead of ScrollView
                ScrollView {
                    ForEach(1..<100) { index in
                        HStack {
                            Spacer()
                            Text("\(index)")
                            Spacer()
                        }
                    }
                }.background(Color.red)
                HStack {
                    Spacer()
                    Text("Test")
                    Spacer()
                }
                .background(Color.red)
            }
            .navigationBarTitle(Text("Test"), displayMode: .inline)
        }
    }
}
user12208004
  • 1,704
  • 3
  • 15
  • 37

2 Answers2

7

You have to pass 0 for no spacing. By default it takes default space based on context

VStack(spacing: 0) {

   // the same result with using List instead of ScrollView
   ScrollView {

   .........
}
Kishan Bhatiya
  • 2,175
  • 8
  • 14
  • 1
    I tried this and this isn’t the case? There’s still a gap between the bottom of the List view and the next view whether or not it’s embedded in a VStack with 0 spacing. See here because I’m trying to solve something similar. https://stackoverflow.com/questions/67579763/how-to-have-2x2-made-of-squares-fit-screen-in-swiftui – Petesta May 18 '21 at 21:03
0

Just use GeometryReader

@State var contentSize: CGSize = .zero

ScrollView {
    YourContentView()
        .overlay(
            GeometryReader { geo in
                Color.clear.onAppear {
                    contentSize = geo.size
                }
            }
        )
}
.frame(maxWidth: .infinity, maxHeight: contentSize.height)
Viktoria
  • 119
  • 6