0

I want to change views once the user taps 'get started' but due to having navigation view in my first view, it is showing back button on my next screen which I don't want. Please see the images attached below.

Code for the first view is below: import SwiftUI

struct ContentView: View {

var body: some View {
    NavigationView {
        VStack {
            Spacer()
            Text("LifePath")
                .font(.system(size: 48, weight: .semibold))
                .padding(.bottom)
            Spacer()
            
            
            
            NavigationLink(destination: ViewChanger()) {
                Text("Get Started")
                    .font(.headline)
                    .navigationBarBackButtonHidden(true)
            }
        }
        .padding()
    }
}

}

back button showing on screen 2

First view

2 Answers2

0

Change the location of your navigationBackButtonHidden modifier so that it actually modifies the view that you're going to (and not the NavigationLink label):

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                Text("LifePath")
                    .font(.system(size: 48, weight: .semibold))
                    .padding(.bottom)
                Spacer()
                
                
                
                NavigationLink(destination: ViewChanger()
                                .navigationBarBackButtonHidden(true) // <-- Here
                ) {
                    Text("Get Started")
                        .font(.headline)
                        
                }
            }
            .padding()
        }
    }
}

If you want not only the back button to be gone, but the entire header bar, you can use the .navigationBarHidden(true) modifier.

Also, if you run this on iPad at all, you probably want .navigationViewStyle(StackNavigationViewStyle()) added to the outside of your NavigationView

jnpdx
  • 45,847
  • 6
  • 64
  • 94
0

If you use a NavigationLink (in a NavigationView), the view will be pushed. If you want to replace the view, you can do this with an if statement.

For example, this could be implemented like this:

struct ContentView: View {
    @State var showSecondView: Bool = false

    var body: some View {
        if !showSecondView {
            NavigationView {
                VStack {
                    Spacer()
                    Text("LifePath")
                        .font(.system(size: 48, weight: .semibold))
                        .padding(.bottom)
                    Spacer()
            
                    Button(action: { showSecondView = true }) {
                        Text("Get Started")
                            .font(.headline)
                    }
            }
            .padding()
        } else {
            TabView {
                // ...
            }
        }
    }
}
Johannes
  • 564
  • 2
  • 7