gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:40:41 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com