Hurobaki
Hurobaki

Reputation: 4078

Xcode 11 Beta 5 - Modal triggers only once

I just upgrade to Xcode 11 Beta 5 and update my SwiftUI project.

In previous version I wanted to use PresentationLink component to show up a modal. 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 I tried my chance by upgrading to Beta 5 but still no luck.

I noticed that this behaviour seems to be caused by wrapping in a ScrollView component. If I delete the ScrollView component everything works fine as expected.

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.

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

Upvotes: 0

Views: 879

Answers (1)

Zain
Zain

Reputation: 1679

This is the same issue as https://stackoverflow.com/a/57087399/3179416

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

Video showing the ability to use the modal multiple times

Upvotes: 1

Related Questions