ScrollView 中断 .sheet 表示

ScrollView breaks .sheet representation

我正在尝试将 sheet 表示作品嵌套在 ScrollView 内的多个视图中。

没有 ScrollView.sheet 修饰符可以正常工作,但是当我将所有内容包装在滚动视图中时,修饰符仅触发一次。所以在第一次点击时 sheet 看起来很好,但是在关闭它之后我无法再次触发它。我不确定这是 SwiftUI 本身的错误还是我在这里做了什么。

注意:如果我将 .sheet 修饰符添加到 ScrollView 本身,一切正常。但对于我的用例,.sheet 修饰符被添加到 ScrollView 内的自定义视图中深度嵌套。

我正在使用 Xcode Beta 5

没有 ScrollView - 有效

struct SheetWorks: View {
    @State var showSheet = false

    var strings = [
      "Hello", "World", "!"
    ]

    var body: some View {
        HStack {
            ForEach(strings) { string in
                Button(action: {self.showSheet.toggle()}) {
                    Text(string)
                }
                .sheet(isPresented: self.$showSheet) {
                    Text("Here is the sheet")
                }
            }
        }
        .padding()
    }
}

使用 ScrollView - 不起作用

struct SheetDoesntWork: View {
    @State var showSheet = false

    var strings = [
      "Hello", "World", "!"
    ]

    var body: some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(strings) { string in
                    Button(action: {self.showSheet.toggle()}) {
                        Text(string)
                    }
                    .sheet(isPresented: self.$showSheet) {
                        Text("Here is the sheet")
                    }
                }
            }
            .padding()
        }
    }
}

也许有人经历过类似的事情或者可以指出正确的方向。我真的很感激任何帮助。

编辑:这个问题在 Beta 6 中仍然存在

在这里看看我的回答:

基本上你应该只在循环外使用一个 .sheet 并根据本地变量动态打开所需的视图。

var covers = coverData
var selectedTag = 0

Group {
   ForEach(covers) { item in
      Button(action: { 
         self.selectedTag = item.tag
         self.isPresented.toggle() 
      }) {
        CoverAttributes(
           title: item.title,
           alternativeTitle: alternativeTitle,
           tapForMore: item.tapForMore,
           color: item.color,
           shadowColor: item.shadowColor)
      }
   }
}
.sheet(isPresented: self.$isPresented, content: { 
    Text("Destination View \(self.selectedTag)") 
    // Here you could use a switch statement on selectedTag if you want
})

Xcode 11.1 GM Seed 开始,我可以批准 .sheet 修饰符现在可以在 ScrollView 中正常工作。此外,现在还可以正确呈现多行 Text