GuardianKnight
GuardianKnight

Reputation: 63

SwiftUI: How to display the second Sheet when first sheet is closed

I want to make a side menu with .fullScreenCover, when a user is logged in and press MyGarage button. the .fullScreenCover will dismiss and the main view will navigate to MyGarage View. But if user is not logged in the .fullScreenCover will dismiss and a loginView with .fullScreenCover will appear. My problem is, the .fullScreenCover will not work if I put 2 same .fullScreenCover inside the main view. Is there any way to solve this? I'm sorry it's a little bit difficult for me to explain.

Here's the code

SideMenuView

struct SideMenuView: View {
    @Environment(\.presentationMode) var presentationMode
    @Binding var showMyGarage: Bool
    @Binding var showSignIn: Bool
    var user = 0 //If user is 1, it is logged in
    var body: some View {
        NavigationView{
            VStack{
                Button(action: {
                    presentationMode.wrappedValue.dismiss()
                    if user == 1 {
                        self.showMyGarage = true
                    }else{
                        self.showSignIn = true
                    }
                }, label: {
                    Text("My Garage")
                })
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarItems(leading:
                                    HStack(spacing: 20){
                                        Button(action: {
                                            presentationMode.wrappedValue.dismiss()
                                        }, label: {
                                            Text("X")
                                        })
                                        Text("Main Menu")
                                    }
                )
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

MainView

struct HomeView: View {
    @State var showSideMenu = false
    @State private var showMyGarage = false
    @State var showSignIn = false

    var body: some View {
        VStack{

            Text("Home")
            
            NavigationLink(destination: MyGarageView(showMyGarage: $showMyGarage), isActive: $showMyGarage){
                EmptyView()
            }
            
        }
        .navigationBarItems(leading:
                                Button(action: {
                                    self.showSideMenu.toggle()
                                }, label: {
                                    Text("Menu")
                                })
            )
        .fullScreenCover(isPresented: $showSideMenu, content: {
            SideMenuView(showMyGarage: $showMyGarage, showSignIn: $showSignIn)
        })
        .fullScreenCover(isPresented: $showSignIn, content: {
            SignInView()
        })

    }
}

struct MyGarageView: View {
    @Binding var showMyGarage: Bool
    var body: some View {
        Text("MyGarage")
    }
}

struct SignInView: View {
    var body: some View {
        Text("Sign In")
    }
}

Upvotes: 1

Views: 442

Answers (1)

Asperi
Asperi

Reputation: 258441

Try to attach them to different views, like

var body: some View {
    VStack{

        Text("Home")
          .fullScreenCover(isPresented: $showSideMenu, content: {
             SideMenuView(showMyGarage: $showMyGarage, showSignIn: $showSignIn)
          })
        
        NavigationLink(destination: MyGarageView(showMyGarage: $showMyGarage), isActive: $showMyGarage){
            EmptyView()
        }
        .fullScreenCover(isPresented: $showSignIn, content: {
           SignInView()
        })
    }
    .navigationBarItems(leading:
                            Button(action: {
                                self.showSideMenu.toggle()
                            }, label: {
                                Text("Menu")
                            })
        )
}

Upvotes: 1

Related Questions