0

I have 3 Views, let say view1, view2 and view3.

view1 is home screen.

Now, I am presenting view2 with naviagationView.

Now, on some event, I have pushed view 3 from view2.

So in short, scene is view1(home) -> presented NavigationView(view2) -> pushed view3

Now, from view3, I want to dismiss navigation view and wanted to come back to view1(home).

If I am taking presentationMode environment variable and make call as presentationMode.wrappedValue.dismiss, then it is popping up to view2, but I wanted to dismiss the whole navigationview.

Here, I have just pushed one view. But there are chances that I might have pushed 7-8 views and wanted to dismiss the full navigation view from there.

Is there any way to do this in swiftUI?

2
  • 1
    old NavigationView is so limited to use. For your case, init another NavigationView, so the whole stack will be replaced. No way to dismiss to root view. Since iOS 16, there's NavigationStack, you can drop support iOS 16 priors and take advantage of that new API. Commented Nov 10, 2022 at 7:02
  • @QuangHà Thanks for the answer, any workaround for older NavigationView. My app has support for iOS 14+. Commented Nov 10, 2022 at 7:17

2 Answers 2

1

The other day I realised that when you use a NavigationStack or a NavigationView, and you start presenting views using NavigationLink, if you long press the back button, it displays a menu with the names of all the previous views so you can choose where to go back. So if you have something like this with four levels:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Go to second view") {
                NavigationLink("Go to third view") {
                    NavigationLink("Go to forth view"){
                        Text("The end")
                    }.navigationTitle("Third View").navigationBarTitleDisplayMode(.inline)
                }.navigationTitle("Second View").navigationBarTitleDisplayMode(.inline)
            }.navigationTitle("Home View").navigationBarTitleDisplayMode(.inline)
        }
    }
}

You will see this small menu

I also found this article https://www.macrumors.com/guide/ios-14-hidden-features/ and it seems to be a iOS 14 feature. The problem is that if your app is set to work with previous version of iOS, you will have to find another way to do it.

Sign up to request clarification or add additional context in comments.

1 Comment

I cant ask user to long press that. I wanted just tap and dismiss the whole navigation stack. You have presented just the feature of iOS 14, nothing else.
0

Try this approach

struct ContentView: View {
    @State var id = UUID()
    
    var body: some View {
        NavigationView {
            NavigationLink {
                NavigationLink  {
                    VStack {
                        Text("Third")
                        Button {
                            id = UUID()
                        } label: {
                            Text("Clear Stack")
                        }
                    }.navigationTitle("Third")
                } label: {
                    Text("Go Third")
                }.navigationTitle("Second")
            } label: {
                Text("Go Second")
            }.navigationTitle("First")
        }.id(id)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.