swiftuiswiftui-navigationlink

Using SwiftUI. My Slider/Side-menu launches new Views just fine when clicked but click <back> button and now all the options are 'dead'


Using SwiftUI and a slider/side menu tutorial that I have augmented in order to put actions on each of the side menu selections.

When the side menu is displayed and I tap a menu option, it works great and takes me to a new view with a menu item. But when i tap on and see the side menu still in place, all the menu items are not dead. The menu items still animate a click (with a flicker) but nothing happens. I have to close the side menu, reopen it, and then the menu items work once again - one time.

Can anyone tell me why this is happening?

Here is the pretty contentview, the mainview, and the sidemenu view.

//ContentView.swift

import SwiftUI

struct ContentView: View {

    @State var showMenu = false

var body: some View {

    let drag = DragGesture()
        .onEnded {
            if $0.translation.width < -100 {
                withAnimation {
                    self.showMenu = false
                }
            }
        }

    return NavigationView {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                MainView(showMenu: self.$showMenu)
                    .frame(width: geometry.size.width, height: geometry.size.height)
                    .offset(x: self.showMenu ? geometry.size.width/2 : 0)
                    .disabled(self.showMenu ? true : false)
                if self.showMenu {
                    MenuView()
                        .frame(width: geometry.size.width/2)
                        .transition(.move(edge: .leading))
                }
            }
                .gesture(drag)
        }
            .navigationBarTitle("Side Menu", displayMode: .inline)
            .navigationBarItems(leading: (


                   Button(action: {
                        withAnimation {
                            self.showMenu.toggle()
                        }
                    }) {
                        Image(systemName: "line.horizontal.3")
                            .imageScale(.large)
                    }
                ))
        }
    }
}

struct MainView: View {

    @Binding var showMenu: Bool

    var body: some View {
        Button(action: {
            withAnimation {
               self.showMenu = true
            }
        }) {
            Text("Show Menu")
        }
    }
}

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

and here is the sidemenu view.

//MenuView.swift

import SwiftUI


struct PlayerView: View {
    @State var showMenu = true
    //@EnvironmentObject var session: SessionStore

    var body: some View {
        VStack{
            //self.showMenu = true
            Text("Manage Players Here").foregroundColor(.red)

         }
    }
}


struct MenuView: View {
    @State var showMenu = true


    var body: some View {

        VStack(alignment: .leading) {

            HStack() {

                NavigationLink(destination: PlayerView()) {
                    HStack(){
                        Image(systemName: "person")
                           .foregroundColor(.gray)
                           .imageScale(.large)
                        Text("Players")
                        .foregroundColor(.gray)
                        .font(.headline)
                    }
                }
            }
            .padding(.top, 100)

            }
            .padding()
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(Color(red: 32/255, green: 32/255, blue: 32/255))
            .edgesIgnoringSafeArea(.all)
    }
}


struct MenuView_Previews: PreviewProvider {
    static var previews: some View {
        MenuView()
    }
}

    enter code here

Solution

    1. Binding var in the MenuView

    2. OnAppear{} with Zstack to turn off the showMenu

       import SwiftUI
       struct ContentView: View {
           @State var showMenu = false
      
           var body: some View {
      
               let drag = DragGesture()
                   .onEnded {
                       if $0.translation.width < -100 {
                           withAnimation {
                               self.showMenu = false
                           }
                       }
                   }
      
               return NavigationView {
                   GeometryReader { geometry in
                       ZStack(alignment: .leading) {
                           MainView(showMenu: self.$showMenu)
                               .frame(width: geometry.size.width,      height: geometry.size.height)
                               .offset(x: self.showMenu ?      geometry.size.width/2 : 0)
                               .disabled(self.showMenu ? true : false)
                           if self.showMenu {
                               MenuView(showMenu: self.$showMenu)
                                   .frame(width: geometry.size.width/2)
                                   .transition(.move(edge: .leading))
                           }
                       }
                       .gesture(drag).onAppear {
                           self.showMenu = false
                       }
                   }
                   .navigationBarTitle("Side Menu", displayMode:       .inline)
                   .navigationBarItems(leading: (
      
      
                       Button(action: {
                           withAnimation {
                               self.showMenu.toggle()
                           }
                       }) {
                           Image(systemName: "line.horizontal.3")
                               .imageScale(.large)
                       }
                   ))
               }
           }
       }
      
       struct MainView: View {
      
           @Binding var showMenu: Bool
      
           var body: some View {
               Button(action: {
                   withAnimation {
                       self.showMenu = true
                   }
               }) {
                   Text("Show Menu")
               }
           }
       }
      
      
      
      
       struct PlayerView: View {
           @State var showMenu = true
           //@EnvironmentObject var session: SessionStore
      
           var body: some View {
               VStack{
                   //self.showMenu = true
                   Text("Manage Players Here").foregroundColor(.red)
      
               }
           }
       }
      
      
       struct MenuView: View {
           @Binding var showMenu: Bool // = true
      
      
           var body: some View {
      
               VStack(alignment: .leading) {
      
                   HStack() {
      
                       NavigationLink(destination: PlayerView()) {
                           HStack(){
                               Image(systemName: "person")
                                   .foregroundColor(.gray)
                                   .imageScale(.large)
                               Text("Players")
                                   .foregroundColor(.gray)
                                   .font(.headline)
                           }
                       }
                   }
                   .padding(.top, 100)
      
               }
               .padding()
               .frame(maxWidth: .infinity, alignment: .leading)
               .background(Color(red: 32/255, green: 32/255, blue:         32/255))
               .edgesIgnoringSafeArea(.all)
      
           }
       }