2

I am trying to attach multiple modal views to a NavigationView using .sheet presentation.

I tried chaining the .sheet together just to discover that only the last one can be triggered to display when bind variable is changed

Is there a way to do this for multiple modals?

var body: some View {
NavigationView {
     ZStack(alignment: .leading) {
             MainView()
                }
                // present profile page
                .sheet(isPresented: self.$presentation.profile){
                    ProfilePage()
                }

                // present product page
                .sheet(isPresented: self.$presentProduct) {
                    SingleProductView()
                }

                //present login
                .sheet(isPresented: self.$showLogin) {
                    LoginView(showLogin:self.$showLogin)
                }

                //present cart
                .sheet(isPresented: self.$showCart) {
                    CartView()
                }

                // set title
                .navigationBarTitle("Title", displayMode: .inline)

                // set items
                .navigationBarItems(leading: (
                    NavigationBarLeadingItems()
                ),trailing: (
                    NavigationBarTrailingItems()
                    )
                )

Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31
  • Attach .sheet to different views or use one .sheet that conditionally show different content. Sequence of sheets do not work as you already found. – Asperi Mar 13 '20 at 13:09

1 Answers1

5

Calling the same method multiple times on the same Element in SwiftUI will always result in only the last one being applied.

Text("Some Nice Text")
    .forgroundColor(.red)
    .forgroundColor(.blue)

This will always result in the Text to be displayed in blue and not in red, even thou you set its color to red. Same gos for you .sheet call. The last .sheet call will kinda override its predecesors.

There are two possible solutions:

you place the .sheet call on different Views.

ViewOne().sheet(...) { ... }
ViewSecond().sheet(...) { ... }

you change the content of you .sheet call dynamically:

View()
    .sheet(...) {
        if someState {
            return SheetViewOne()
        else {
            return SheetViewSecond()
        }
    }
 }
KevinP
  • 2,562
  • 1
  • 14
  • 27
  • that works thanks, but i guess that means the views are not stacked on top of each other, which is ok but wouldn't it be nice if its possible – Nic Wanavit Mar 14 '20 at 05:00