xcode - SwiftUI:listRowInsets 未按预期工作

标签 xcode swiftui swiftui-list

listRowInsets(_:)使用 List() 时未按预期工作和 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))
        }
    }
}

结果:

Example 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))
        }
    }
}

结果:

Example 2

最佳答案

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

以下作品

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))
        }
    }
}

关于xcode - SwiftUI:listRowInsets 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929162/

相关文章:

swift - UIKit - 日历组件返回负年份的绝对值

swift - 一对 DatePickers SwiftUI

ios - 每次从 SwiftUI 中的详细 View 返回时,如何阻止 View 刷新?

swiftui - 如何在 TabView SwiftUI 中保存 View 的列表状态

iOS:本地化自白

ios - Sprite Kit 场景不呈现

ios - iPhone 应用程序定位

ios - SwiftUI 主列表可滚动标题 View 没有部分?

swift - 通过大数组 Swift 进行过滤

ios - 将 Storyboard项目的内容(包括约束)复制到 xib 文件