从 SwiftUI 的列表中删除默认填充

eja*_*a08 5 swift swiftui

默认情况下,使用 ScrollView 时,其中的视图分布在整个屏幕宽度上,但使用 List 时,两侧有填充。有没有办法摆脱这种填充?

Asp*_*eri 13

要实现这一点,您需要将ForEachinsideList.listRowInsets下面的示例结合使用

演示

struct Demo: View {
    var colors: [Color] = [.red, .blue, .yellow]
    var body: some View {
        List {
            ForEach(colors, id: \.self) { color in
                color
            }.listRowInsets(EdgeInsets())
        }
    }
}
Run Code Online (Sandbox Code Playgroud)