list - SwiftUI - 使列表分隔线扩展到触摸屏边缘

标签 list swiftui

我正在使用 SwiftUI 中的列表。我无法弄清楚如何使行分隔符边到边。我已经尝试过 .edgesIgnoringSafeArea,但这似乎并不能解决问题。见下图:

enter image description here

注意列表线如何触及右边缘,而不是左边缘。

最佳答案

似乎没有合适的解决方案来特别控制分隔符,因此您必须有自己的解决方法。

我建议使用 EdgeInsets 来扩展行以到达边缘,因此分隔符。

然后您可以将您想要的填充添加到行的内容。

List(0..<10) { _ in
     HStack {
          Text("title")
           Spacer()
     }.background(Color.gray)
      .padding(.horizontal, 20)
 }.padding(EdgeInsets(top: 0, leading: -20, bottom: 0, trailing: -20))

最后的结果是这样的:

And the final result would be something like this:

您还可以通过替换

进一步简化它
.padding(EdgeInsets(top: 0, leading: -20, bottom: 0, trailing: -20))

.padding(.horizontal, -20)

你可以为每一行设置不同的 padding/Inset,下面是 Apple 的 listRowInsets 文档中的代码片段

struct ContentView: View {
    enum Flavor: String, CaseIterable, Identifiable {
        var id: String { self.rawValue }
        case vanilla, chocolate, strawberry
    }

    var body: some View {
        List {
            ForEach(Flavor.allCases) {
                Text($0.rawValue)
                    .listRowInsets(.init(top: 0,
                                         leading: 25,
                                         bottom: 0,
                                         trailing: 0))
            }
        }
    }
}

关于list - SwiftUI - 使列表分隔线扩展到触摸屏边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68424553/

相关文章:

SwiftUI 列表和 OnAppear

uitableview - SwiftUI:内容与中心对齐的自定义表格 View 单元格

python - 通过键列表访问嵌套字典项?

python - 如何在 Python 中正确合并重叠的日期时间范围

python - 为什么Python列表没有范围限制(而整数有)?

java - Array 到 List 或 List 到 Array,哪个转换更快?

swift - 使用 SwiftUI 时尝试在 View 上显示工作表时应用程序崩溃

ios - SwiftUI 导航栏和状态栏 - 让它们颜色相同

swift - 尝试重新创建 SwiftUI 列表演示时出现错误 "Unable to infer complex closure return type; add explicit type to disambiguate"

python - 如何在字典值是列表项上使用计数器