Tobias Timpe
Tobias Timpe

Reputation: 770

SwiftUI: Presenting sheet from VStack item doesn't work on iOS 14

I am trying to present a sheet from an item in a VStack. The following code works on iOS 13.7 but not on iOS 14:

import SwiftUI

struct ListRow: View {
    @State var showingSheet: Bool = false
    var body: some View {
        Button(action: {
            self.showingSheet = true
        }) {
            Text("Tap me")
        }.sheet(isPresented: self.$showingSheet, content: {
            NavigationView {
                Text("Hello")
            }
        })
    }
}

struct ListRow_Previews: PreviewProvider {
    static var previews: some View {
        ListRow()
    }
}



struct ContentView: View {
    @State private var showingModal: Bool = false
    var body: some View {
        NavigationView{
            VStack {
                ForEach (0..<10) {_ in
                    ListRow().padding()
                }
            }.navigationBarItems(leading: Button(action: {
                self.showingModal = true
            }, label: {
                Text("TAP")
            })).sheet(isPresented: self.$showingModal, content: {
                Text("Testing main modal")
            })
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

However, when I replace the VStack with a List, it seems to work.

Is this intended behaviour or a bug in SwiftUI on iOS 14? What am I doing wrong?

Thanks

Tobias Timpe

Upvotes: 6

Views: 2079

Answers (1)

Nicolas Degen
Nicolas Degen

Reputation: 1846

I think it the issue is presenting .sheet() twice on the same view.

As VStack is non-dynamic, I expect that the .sheet of the ListRow and the VStack are applied to the same construct under the hood, just like you'd call it twice on the upper level:

TextView("Foo")
.sheet(isPresented: $showA) { Text("A") }
.sheet(isPresented: $showB) { Text("B") } // Does not work, only one sheet is allowed

So either you show only one sheet and have logic in the sheet whether to show "Hello" or "Testing main modal" or you use a List:)

Upvotes: 5

Related Questions