'Remove default padding from List in SwiftUI

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?



Solution 1:[1]

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

backup

Solution 2:[2]

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2