Remove default padding from List in SwiftUI

Viewed 5048

When using ScrollView the views inside it are spread across the whole screen width by default, but when using List, there is a padding on the sides. Is there a way to get rid of this padding?

2 Answers

To achieve this you need to use ForEach inside List combined with .listRowInsets as in example below

demo

struct Demo: View {
    var colors: [Color] = [.red, .blue, .yellow]
    var body: some View {
        List {
            ForEach(colors, id: \.self) { color in
                color
            }.listRowInsets(EdgeInsets())
        }
        // Update: Different iOS versions have different 
        // default List styles, so set explicit one if needed
        .listStyle(PlainListStyle())
    }
}

For iOS 15.0+, you may try listStyle modifier and set it as plain.

var body: some View {
        List {
            // some rows
        }
        .listStyle(.plain)
    }
Related