Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swiftui

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

like image 480
Tobias Timpe Avatar asked Sep 21 '20 07:09

Tobias Timpe


Video Answer


1 Answers

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

like image 191
Nicolas Degen Avatar answered Oct 16 '22 14:10

Nicolas Degen