-2

I have 3 views - ContentView(), View1(), View2(). ContentView() being the main view is embedded in the NavigationView. It has a button that opens View1() MODALLY using .sheet(). View1 is also embedded in a new NavigationView and has a NavigationLink that navigates to View2().

The problem is I want to present View2() as a new stack but it is getting presented modally like View1().

Note: I had to embed View1() in a new NavigationView coz if I don't then NavigationLinks won't work on it.

import SwiftUI

struct ContentView: View {
    @State private var isPresentingView1 = false

    var body: some View {
        NavigationView {
            VStack {
                Button("Open View1 Modally") {
                    isPresentingView1.toggle()
                }
                .sheet(isPresented: $isPresentingView1) {
                        View1()
                }
            }
        }
    }
}

struct View1: View {
    var body: some View {
        NavigationView{
            NavigationLink("Open View2", destination: View2())
                .navigationBarTitle("View 1")
        }
    }
}

struct View2: View {
    var body: some View {
        VStack {
            Text("View 2 Content")
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32
Uday Agarwal
  • 7
  • 1
  • 3
  • Please show a [mcve]. Show your code please. – Sweeper Aug 12 '23 at 08:20
  • Added for your reference. – Uday Agarwal Aug 12 '23 at 08:36
  • Cannot reproduce. `View2` is not presented modally. Can you show a screenshot of what you are getting? – Sweeper Aug 12 '23 at 08:54
  • Exactly. View2() is not presented modally still, it is shown modally. I don't know how to provide a screenshot but it is using the same navigation view of View1(). – Uday Agarwal Aug 12 '23 at 09:04
  • And you want it to use the navigation view of `ContentView` instead? – Sweeper Aug 12 '23 at 09:16
  • You can use another `sheet` in `View1` to present as a second modal. The way you have it at present will push `View2` onto the`View1` navigation stack. I’m not sure what else you might be trying to achieve. – Chris Aug 12 '23 at 09:25
  • 2
    You would have to dismiss the seat and then use `NavigationLink` with `isActive` to trigger `View2`. But if you can switch to `NavigationStack` it would simplify the process with `navigationDestination` – lorem ipsum Aug 12 '23 at 12:43
  • Another vote for using `NavigationStack` – koen Aug 12 '23 at 14:41

1 Answers1

0

I found the solution. Posting it if anyone is facing the same problem. There is an onDismiss modifier in ".sheet" which runs after the sheet is dismissed. Using that, I was able to achieve the desired functionality.

struct ContentView: View {
    @State private var isPresentingView1 = false
    @State private var isNewViewPresented = false

    var body: some View {
        NavigationView {
            VStack {
                Button("Open View1 Modally") {
                    isPresentingView1.toggle()
                }
                .sheet(isPresented: isPresentingView1, onDismiss: {
                    isNewViewPresented = true
                }) {
                    View1()
                }
                .background(
                    NavigationLink("", destination: View2(), isActive: $isNewViewPresented)
                )
            }
        }
    }
}
Uday Agarwal
  • 7
  • 1
  • 3