Navigating to New Views in a Split View (iPad)

306 Views Asked by At

Here is a split view:

enter image description here

If I click on the "Toggle", I'd like to see

enter image description here

And If I click Toggle again, show Left 1 - Right 1 again, and so on.

Here is my code:

struct ContentView2: View {
    @State var toggle : String

    var body: some View {
        NavigationView {
           
            if toggle == "first" {
                LHSTest1()
                RHSTest1()
            }
            else {
                LHSTest2()
                RHSTest2()
            }
        }
    }
}

struct LHSTest1: View {
    @State private var isActiveForLHS1 = false
    var body: some View {
        VStack {
            Button("Toggle") {
                self.isActiveForLHS1 = true
            }
            .padding()
            Text("Left 1")
            NavigationLink(destination: ContentView2(toggle: "second"), isActive: $isActiveForLHS1) {    }.opacity(0)
        }

    }
}

struct RHSTest1: View {
    var body: some View {
        Text("Right 1")
    }
}

struct LHSTest2: View {
    @State private var isActiveForLHS2 = false
    var body: some View {
        VStack {
            Button("Toggle") {
                self.isActiveForLHS2 = true
            }
            Text("Left 2")
            NavigationLink(destination: ContentView2(toggle: "first"), isActive: $isActiveForLHS2) {    }.opacity(0)
        }

     }
}

struct RHSTest2: View {
    var body: some View {
        Text("Right 2")
    }
}

Here is the problem: When I click toggle, a new layer of navigation view appears:

enter image description here

Any thoughts will be greatly appreciated.

0

There are 0 best solutions below