11

Im trying to link a button action in SomeView1() to navigate to a someView2() without having the back button at the top of the screen. Instead, I want to add another button in SomeView2() that will navigate back to SomeView1(). is this possible in SwiftUI yet?

SomeView1()

struct SomeView1: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView2()) {
                    Text("go to SomeView2")
                }
                Spacer()
            }
        }
    }
}

SomeView2()

struct SomeView2: View {
    var body: some View {
        NavigationView {
            VStack {
                //...view's content

                NavigationLink(destination: SomeView1()) {
                    Text("go to SomeView1")
                }
                Spacer()
            }
        }
    }
}

this is what it looks like: enter image description here

Liv
  • 235
  • 3
  • 9

3 Answers3

17

The right way to get what you want here is to use the presentationMode environment variable:

import SwiftUI

struct View2: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        VStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("POP")
            }
        }
        .navigationBarTitle("")
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: View2()) {
                Text("PUSH")
                    .navigationBarTitle("")
                    .navigationBarHidden(true)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
superpuccio
  • 11,674
  • 8
  • 65
  • 93
1

I believe that you should use only one NavigationView for the whole navigation process. Now you have three NavigationViews inside each other, which produces three back buttons.

So in your case it would become something like this:

struct SomeView1InsideNavigationView: View { // This should be the first view you present
    var body: some View {
        NavigationView { // Use NavigationView only once
            SomeView1()
        }
    }
}

struct SomeView1: View {
    var body: some View {
        VStack { // Do *not* use NavigationView here
            //...view's content

            NavigationLink(destination: SomeView2()) {
                Text("go to SomeView2")
            }
            Spacer()
        }
    }
}

struct SomeView2: View {
    var body: some View {
        VStack { // Do *not* use NavigationView here
            //...view's content

            NavigationLink(destination: SomeView1()) {
                Text("go to SomeView1")
            }
            Spacer()
        }
    }
}
cbjeukendrup
  • 3,216
  • 2
  • 12
  • 28
0

You can do something like this in SomeView2():

NavigationView {
   VStack {
            //...view's content

        NavigationLink(destination: SomeView1()) {
            Text("go to SomeView1")
        }
        Spacer()
    }
}.navigationBarBackButtonHidden(true)
39fredy
  • 1,923
  • 2
  • 21
  • 40
  • This does hide the back button but it still pushes the view down the screen when you navigate (as if its still making room for the back button). after navigating back and forth between the 2 views, the views end up half way down the screen – Liv Oct 25 '19 at 17:44
  • you can also try using `.navigationBarHidden(true)` – 39fredy Oct 25 '19 at 17:45
  • i still get the same problem. i added a picture for reference – Liv Oct 25 '19 at 17:49
  • could there be another way of doing this other than NavigationLink? – Liv Oct 25 '19 at 17:50
  • 1
    This answer seems the most sensible, particularly if there are multiple navigation links within the NavigationView for which identical behaviour is required. However, I found that what was needed was .navigationBarHidden(true) – RudyF Dec 08 '21 at 05:54