SwiftUI Navigation after login

Viewed 1375

I have prepared 2 views which are signing and also the home view. I tried to pop/hide the signing view after the user signing to the home view.

The problem now is the view is allow the user to click the back button to go back to the login view. I have no idea how to settle this. Can anyone give me some hints?

Here is my navigation code for login:

NavigationLink(destination: HomePageView(), tag: 1, selection: $selection) {
    Button(action: {
        print("Register tapped")
        self.verify()
        self.selection = 1
    }) {
        HStack {
            Text("OK").foregroundColor(Color.white).bold().foregroundColor(.white)
                .frame(width: UIScreen.main.bounds.width - 30, height: UIScreen.main.bounds.height / 12)
                .background(Color.orange)
                .cornerRadius(35.0)
                .font(.headline)
                .padding()
        }
    }
}
2 Answers

There should only be one NavigationView per navigation stack. You need to remove all nested NavigationViews except the top one. In your child views you still can modify the top one.

Here is a simple demo:

struct LoginView: View {
    @State private var active: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: HomeView(), isActive: self.$active) {
                    Text("Register")
                }
            }
            .navigationBarTitle("Login View")
        }
    }
}

struct HomeView: View {
    var body: some View {
        Text("inside home view")
            .navigationBarTitle("Home view")
            .navigationBarBackButtonHidden(true)
    }
}

Tested in Xcode 11.6, iOS 13.6.

Related