gpt4 book ai didi

swiftui - ScrollView 破坏了 .sheet 表示

转载 作者:行者123 更新时间:2023-12-02 18:23:25 26 4
gpt4 key购买 nike

我正在尝试将工作表表示工作嵌套在 ScrollView 内的多个 View 中。

如果没有 ScrollView.sheet 修饰符可以正常工作,但是当我将所有内容都包含在 ScrollView 内时,修饰符只会触发一次。因此,第一次点击时,工作表看起来很好,但在关闭它后,我无法再次触发它。我不确定这是否是 SwiftUI 本身的错误,或者我是否在这里做了什么。

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

我正在使用 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 中仍然存在

最佳答案

看看我的回答:https://stackoverflow.com/a/57259687/554203

基本上,您应该在循环外仅使用一个 .sheet 并根据本地变量动态打开所需的 View 。

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

关于swiftui - ScrollView 破坏了 .sheet 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57546433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com