移除列表中的黑色填充物(SwiftUI)
这是我名单上的代码:移除列表中的黑色填充物(SwiftUI),swift,list,listview,swiftui,padding,Swift,List,Listview,Swiftui,Padding,这是我名单上的代码: import SwiftUI struct PaymentScreen: View { init() { UITableView.appearance().separatorStyle = .none UITableViewCell.appearance().backgroundColor = .black // I can change color of this paddings here } var bod
import SwiftUI
struct PaymentScreen: View {
init() {
UITableView.appearance().separatorStyle = .none
UITableViewCell.appearance().backgroundColor = .black // I can change color of this paddings here
}
var body: some View {
return VStack (alignment: .center) {
List (Terminal.terminals, id: \.self.distance) { terminal in
PayTerminalAdapter(terminal: terminal).background(Color.white)
}
}
}
}
我的列表截图:
我无法删除黑色的前导和尾随填充以及列表元素之间的填充。当我使用ForEach(我有很长的终端阵列)时,我对填充没有问题,但它的工作速度很慢,因此ForEach对我来说是一个不好的选择。如何删除列表中的黑色填充?您应该为此更改
.listRowBackground()
。也许您正在寻找.listRowInsets
修改器
解决方案:
List {
ForEach(Terminal.terminals, id: \.self.distance) {
PayTerminalAdapter(terminal: terminal).background(Color.white)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List(0...100, id: \.self) { _ in
Text("Custom row inset not being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List {
ForEach(0...100, id: \.self) { _ in
Text("Custom row inset being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
在上面,您可能已经注意到列表(数据:)
现在只是列表(内容:)
,并且数据被移动到ForEach(数据:id:)
我不确定是否存在SwiftUI错误,但
.listRowInsets
仅适用于单个项目。不过,这一个项目可以有多个项目,并且仍然可以重复使用,所以不用担心。但是,作为一项测试,尽管以下各项看起来应该有效,但它并没有: 示例(不工作):
List {
ForEach(Terminal.terminals, id: \.self.distance) {
PayTerminalAdapter(terminal: terminal).background(Color.white)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List(0...100, id: \.self) { _ in
Text("Custom row inset not being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List {
ForEach(0...100, id: \.self) { _ in
Text("Custom row inset being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
因此,将数据源放在一个ForEach
中,并在其上应用.listRowInsets
示例(工作):
List {
ForEach(Terminal.terminals, id: \.self.distance) {
PayTerminalAdapter(terminal: terminal).background(Color.white)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List(0...100, id: \.self) { _ in
Text("Custom row inset not being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
List {
ForEach(0...100, id: \.self) { _ in
Text("Custom row inset being applied")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
.listRowInsets(EdgeInsets(top: 1, leading: 2, bottom: 1, trailing: 2))
}
是的,我知道。listRowBackground()。这不是那么容易。那么问题是什么呢?告诉我,这样我就可以看看我是否可以帮助你更合适的查看cell.appearance().backgroundColor=.white-我更改了此填充物的颜色,但无法删除它看看我知道如何更改此填充物的颜色,但我想删除它很好!它起作用了!但不管怎么说,有趣的是,当我只使用List而不使用ForEach时,为什么.listRowInsets()不起作用?@alexbayker我不确定,这可能是
List(数据:id:)
内部工作方式的一个缺陷。如果我弄明白了,我会更新这个答案。如果这个答案有帮助,那么请接受它:)