简体   繁体   English

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

[英]Xcode 11 Beta 5 - Modal triggers only once

I just upgrade to Xcode 11 Beta 5 and update my SwiftUI project. 我只是升级到Xcode 11 Beta 5并更新了我的SwiftUI项目。

In previous version I wanted to use PresentationLink component to show up a modal. 在以前的版本中,我想使用PresentationLink组件显示模式。 I had the same problem than now, the modal has only shown once. 我现在遇到了同样的问题,模态只显示了一次。 I thought it was a bug as I saw in other SO posts. 正如我在其他SO帖子中看到的那样,我认为这是一个错误。 So I tried my chance by upgrading to Beta 5 but still no luck. 因此,我尝试了升级到Beta 5的机会,但还是没有运气。

I noticed that this behaviour seems to be caused by wrapping in a ScrollView component. 我注意到,此行为似乎是由ScrollView组件中的包装引起的。 If I delete the ScrollView component everything works fine as expected. 如果我删除ScrollView组件,一切将按预期工作。

Here's the code: 这是代码:

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

To summarize, without ScrollView wrapper the Modal behaviour works as expected. 总而言之,如果没有ScrollView包装器,则Modal行为将按预期工作。

I would like to know if there is a solution / workaround ? 我想知道是否有解决方案/解决方法? Or I just have to wait a release :) 或者我只需要等待发布:)

Edit from answer : 从答案编辑

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

This is the same issue as https://stackoverflow.com/a/57087399/3179416 这是与https://stackoverflow.com/a/57087399/3179416相同的问题

Just move your .sheet outside of your ForEach . 只需将.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()
            }
        }
}

该视频显示了多次使用模式的能力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM