【问题标题】:SwiftUI ScrollView inside List disappears when scrollingList中的SwiftUI ScrollView在滚动时消失
【发布时间】:2019-12-22 14:56:20
【问题描述】:

如果我将多个水平 ScrollViews 放入 List 内,它们会在滚动时消失。这是一个例子:

这里是重现问题的代码:

struct ContentView: View {

    var body: some View {
        List {
            ForEach(0...100, id: \.self) { _ in
                ScrollView(.horizontal) {
                    HStack {
                        Rectangle().frame(width: 100, height: 100)
                        Rectangle().frame(width: 100, height: 100)
                        Rectangle().frame(width: 100, height: 100)
                    }
                }
            }
        }
    }
}

我使用的是 iOS 13.3。不幸的是,将帧添加到 ScrollViewHStack 并没有帮助。有谁知道解决这个问题的方法吗?

【问题讨论】:

  • 我建议使用垂直ScrollView 而不是List - 没有这个问题(以及其他已知的基于列表的问题)
  • @Asperi A List 如果有很多项目,性能会更好。此外,我不能像List (GroupedListStyle()) 那样设置ScrollView 的样式。所以仅仅使用ScrollView 对我来说并不是一个真正的解决方案。
  • 你有没有设法解决这个问题?
  • @ryansle 不,我还没有找到解决办法。
  • @ThomasVos 正是这个链​​接最终为我解决了这个问题:stackoverflow.com/questions/59810913/…

标签: ios swift listview scrollview swiftui


【解决方案1】:

这似乎是与物品重用有关的错误。您可以尝试修复在 Lists 和 ScrollViews 中设置 id。

List(array, id: \.self) { item in
    Text("\(item)")
}.id(UUID().uuidString)

herehere 中有关此修复的更多信息。

【讨论】:

  • 在我的情况下,只需将 id 设置为滚动视图即可。谢谢你的回答。
  • 非常感谢! @KarenKarapetyan
猜你喜欢
  • 2015-06-20
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
相关资源
最近更新 更多