SwiftUI:listRowInsets 未按预期工作

Moh*_*med 11 xcode swiftui swiftui-list

listRowInsets(_:)使用List()and时没有按预期工作List(Data,id)。在下面的示例 1中,零插入完美地工作,而在示例 2 中它什么也不做。


示例 1:

struct ContentView: View {
    var body: some View {
        List {
            Color.red
                .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
            Color.blue
                .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
            Color.yellow
                .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

示例 1


示例 2

struct ContentView: View {
    var colors: [Color] = [.red, .blue, .yellow]
    var body: some View {
        List(colors, id: \.self) { color in
            color.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

示例 2

Asp*_*eri 11

我认为原因是在使用的构造函数中。在.listRowInsets通过文档效果视图被放置在列表(直接地)。

以下作品

var colors: [Color] = [.red, .blue, .yellow]
var body: some View {
    List {
        ForEach(colors, id: \.self) { color in
            color.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很棒的发现!Fwiw,虽然这个解决方案有效,但我想知道另一种失败方式是否只是一个错误或预期行为。https://developer.apple.com/documentation/swiftui/color/listrowinsets(_:) 讨论中的示例提到像 @Mohammed 那样设置列表插入。 (2认同)