如何制作全宽的 SwiftUI 列表?

How can I made a SwiftUI List full width?

我希望我的 List 是全角的。填充是自动添加的,出于我的目的,我不希望这样。这只是示例代码。

这是我的代码:

struct ViewModel: Identifiable {
    let title: String
    let id = UUID()
}

struct ContentView: View {
    
    let models = [
        ViewModel(title: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
        ]
    
    var body: some View {
        List(models, id: \.id) { model in
            Text(model.title)
                .frame( maxWidth: .infinity)
        }.frame( maxWidth: .infinity).edgesIgnoringSafeArea(.all)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

您看到的列表样式称为 InsetGroupedListStyle。请注意“插入”一词。您可以添加 .listStyle 修饰符以将其更改为没有 insets 的样式:

List(models, id: \.id) { model in
    Text(model.title)
        .frame( maxWidth: .infinity)
}
.frame( maxWidth: .infinity)
.edgesIgnoringSafeArea(.all)
.listStyle(GroupedListStyle()) // or PlainListStyle()