Asp*_*eri 40
这是可能的解决方案的演示。使用 Xcode 12b 测试。
List {
ForEach(0..<3) { _ in
VStack {
Text("Hello, World!").padding(.leading)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.listRowInsets(EdgeInsets())
.background(Color.white)
}
}
Run Code Online (Sandbox Code Playgroud)
ave*_*Joe 10
将@asperi、@akmin 和@zrfrank 答案合并为一件事。根据我的经验,List它比 更可靠、更高效LazyVStack,因此我仍将其用于List任何需要可靠性的复杂情况。
extension View {
func listRow() -> some View {
self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.listRowInsets(EdgeInsets(top: -1, leading: -1, bottom: -1, trailing: -1))
.background(Color(.systemBackground))
}
}
List {
Color.red
.listRow()
Color.green
.listRow()
}
Run Code Online (Sandbox Code Playgroud)
今年,Apple 推出了一种新的修饰符.listRowSeparator,可用于设置分隔符的样式。你可以通过.hidden隐藏它:
List(items, id:\.self) {
Text("Row \($0)")
.listRowSeparator(.hidden)
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过设置将每个分隔符设置为任何颜色,listRowSeparatorTintColor正如我在这个答案中提到的:

按照这里的答案
我如何制作一个适用于 iOS 14 和 iOS 13 的列表,它不显示分隔符和额外的边距
struct NoButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
}
}
struct ListWithoutSepatorsAndMargins<Content: View>: View {
let content: () -> Content
var body: some View {
if #available(iOS 14.0, *) {
ScrollView {
LazyVStack(spacing: 0) {
self.content()
}
.buttonStyle(NoButtonStyle())
}
} else {
List {
self.content()
}
.listStyle(PlainListStyle())
.buttonStyle(NoButtonStyle())
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例用法 -
ListWithoutSepatorsAndMargins {
ForEach(0..<5) { _ in
Text("Content")
}
}
Run Code Online (Sandbox Code Playgroud)
如果列表中有更多组件,请将它们包装在 Group 中
ListWithoutSepatorsAndMargins {
Group {
self.groupSearchResults()
self.myGroups()
self.exploreGroups()
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对某人有所帮助,我在这么小的事情上浪费了很多时间,Apple 正在努力推动我们使用 LazyVStack,看来
我在苹果开发者论坛上找到了这个简单的解决方案。它在 14.4 上对我有用:
List {
...
}.listStyle(SidebarListStyle())
Run Code Online (Sandbox Code Playgroud)
这似乎在边缘周围添加了一点填充。如果这对您来说是个问题,您可以尝试一些负填充。