Xcode 11 Beta 5 - 模态仅触发一次

Xcode 11 Beta 5 - Modal triggers only once

我刚刚升级到 Xcode 11 Beta 5 并更新了我的 SwiftUI 项目。

在以前的版本中,我想使用 PresentationLink 组件来显示模式。我遇到了与现在相同的问题,模态只显示了一次。正如我在其他 SO 帖子中看到的那样,我认为这是一个错误。所以我通过升级到 Beta 5 来尝试我的机会,但仍然没有运气。

我注意到此行为似乎是由 ScrollView 组件中的包装引起的。如果我删除 ScrollView 组件,一切都会按预期正常工作。

代码如下:

struct HomeList : View {

    var listViewItems = listViewItemsData

    @State var show = false

    var body: some View {

        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text("Project title").font(.largeTitle).fontWeight(.heavy)
                    Text("Project subtitle").foregroundColor(Color.gray)
                }
                Spacer()
            }.padding(.top, 78).padding(.leading, 60)

            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 30) {
                    ForEach(listViewItems) { item in
                        GeometryReader { geometry in
                            Button(action: { self.show.toggle()}) {
                                ListView(title: item.title, image: item.image, color: item.color, destination: item.destination)
                                    .rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
                                    .sheet(isPresented: self.$show, content: { InformationView() })
                            }
                        }.frame(width: 246, height: 360)
                    }
                }.padding(30)
                Spacer()
            }.frame(width: UIScreen.main.bounds.width, height: 480)
            Spacer()
        }
    }
}

总而言之,在没有 ScrollView 包装器的情况下,Modal 行为会按预期工作。

我想知道是否有解决方案/解决方法?或者我只需要等待发布:)

根据答案编辑

struct HomeList : View {

    var listViewItems = listViewItemsData

    @State var show = false
    @State var view: AnyView = AnyView(Text(""))

    var body: some View {
        VStack {
            HStack {
                VStack(alignment: .leading) {
                    Text("Project title").font(.largeTitle).fontWeight(.heavy)
                    Text("Project subtitle").foregroundColor(Color.gray)
                }
                Spacer()
            }.padding(.top, 78).padding(.leading, 60)

            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 30) {
                    ForEach(listViewItems) { item in
                        GeometryReader { geometry in
                            Button(action: {
                                self.show.toggle()
                                self.view = item.destination
                            }) {
                                ListView(title: item.title, image: item.image, color: item.color, destination: item.destination)
                                    .rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
                            }
                        }.frame(width: 246, height: 360)
                    }
                }.padding(30)
                Spacer()
            }.frame(width: UIScreen.main.bounds.width, height: 480)
                .sheet(isPresented: self.$show, content: { self.view })
            Spacer()
        }
    }
}

这与

的问题相同

只需将 .sheet 移到 ForEach 之外。

import SwiftUI

struct Testing : View {

    var listViewItems: [Int] = [1, 2, 3]

        @State var show = false


        var body: some View {


            VStack {
                HStack {
                    VStack(alignment: .leading) {
                        Text("Project title").font(.largeTitle).fontWeight(.heavy)
                        Text("Project subtitle").foregroundColor(Color.gray)
                    }
                    Spacer()
                }.padding(.top, 78).padding(.leading, 60)

                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 30) {
                        ForEach(listViewItems, id: \.self) { item in
                            GeometryReader { geometry in
                                Button(action: { self.show.toggle()}) {
                                    Text("Button")
                                        .rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
                                }
                            }.frame(width: 246, height: 360)
                        }
                    }.padding(30)
                    Spacer()
                }.frame(width: UIScreen.main.bounds.width, height: 480)
                .sheet(isPresented: self.$show, content: { Text("Modal") })
                Spacer()
            }
        }
}