Search code examples
swiftswiftuiswiftui-list

How to remove bottom padding of List and ScrollView in SwiftUI


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)
        }
    }
}

Solution

  • You have to pass 0 for no spacing. By default it takes default space based on context. The same result will be for List instead of ScrollView.

    ScrollView(.vertical) {
       VStack(spacing: 0) {
            ForEach(items) { item in
    
            // your code here
    
            }
        }
    }