【问题标题】:How to make two Lists in one View scroll as one page in SwiftUI?如何使一个视图中的两个列表在 SwiftUI 中滚动为一页?
【发布时间】:2020-02-18 00:06:45
【问题描述】:

我正在尝试将两个不同大小的表格添加到 SwiftUI 中的一个视图中。我希望两个表都完全展开,整个视图作为一个滚动。现在我只让两个表格固定大小并分别滚动。

some View {
    VStack {
        Text("Table foo")
        List(foo { item in Text(item.name) })

        Text("Table bar")
        List(bar { item in Text(item.name) })
    }
}

尝试将 VStack 更改为 ScrollView,这让事情变得更糟 - 桌子正在变成一个衬垫。

还尝试将List() 替换为ForEach(),但我失去了我真正想要的List() 的功能。

【问题讨论】:

  • 您要找的不是两个不同的列表!它是一个带有自定义瀑布布局的 collectionView。您可以为此使用 UIKit

标签: swiftui swiftui-list


【解决方案1】:

我认为您想要一个包含多个 ForEach 的列表。或者,您可以将其设为具有多个部分的分组列表。针对您的示例进行了调整,以下概念在多种情况下对我来说效果很好:

struct SwiftUIView: View {
    var body: some View {
        List {
            Section(header: Text("Table foo"))
            {
                ForEach(foo) { item in Text(item.name) }
            }
            Section(header: Text("Table bar"))
            {
                ForEach(bar) { item in Text(item.name) }
            }
        }
        .listStyle(GroupedListStyle())
    }
}

【讨论】:

    【解决方案2】:

    您可以使用Form 在滚动视图中嵌入两个列表。

    参考:https://developer.apple.com/documentation/swiftui/form.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多