0

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.

  • `NavigationView` must by only one per navigation hierarchy - you destination is the same as root so creates another navigation hierarchy in already existed. Just redesign your views to have NavigationView only in root. – Asperi Dec 06 '21 at 06:59
  • From your reply, Asperi, I'm gleaned that calling ContentView2 again creates another navigation hierarchy. I created another view called ContentView3, which is a replica of ContentView2, without the NavigationView. This solved the original problem of multiple navigation hierarchies, but now the split view behaviour is lost (I have only one view). But this is progress - thank you for your help Asperi, much appreciated. – bridgenbarbu Dec 06 '21 at 07:13

0 Answers0