2

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()
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
SAS231
  • 175
  • 4
  • 17
  • Why don't you want to hide back button? There are a lot of examples here how to do that. – Asperi Aug 29 '20 at 07:42
  • Sorry may I know is that a proper way to do so? – SAS231 Aug 29 '20 at 07:54
  • You can do everything which is allowed by public API – Asperi Aug 29 '20 at 07:55
  • @Asperi I tried to hide the back button but the navigation bar is still there. And my home view got another navigation view. Cause there is 2 navigation view. – SAS231 Aug 29 '20 at 08:14
  • @pawello2222 but in my home view it contained the tab bar which containing the NavigationView. So what she I do now. That means I got 2 separate part of code Register and also Home. – SAS231 Aug 29 '20 at 08:48
  • @SAS231 This might help you: [SwiftUI Hide TabView bar inside NavigationLink views](https://stackoverflow.com/questions/61970939/swiftui-hide-tabview-bar-inside-navigationlink-views). You still need to get rid of nested navigation views. My answer below should help to answer your question. If you see the answers in the link above you will know how to combine it with a tab view as well. – pawello2222 Aug 29 '20 at 08:50
  • @pawello2222 tq, so much. Im gonna get a try. Have a nice day – SAS231 Aug 29 '20 at 08:54
  • @pawello2222 hmmm I got another question, how if I want to directly go into the home view in the next time after first time login. But there is no navigation view inside the home view? – SAS231 Aug 29 '20 at 09:03
  • @SAS231 Just set `active` to true or conditionally display two views in one. But better post it as another question altogether. – pawello2222 Aug 29 '20 at 09:05

2 Answers2

1

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.

pawello2222
  • 46,897
  • 22
  • 145
  • 209
0

You seem to need a FullScreenCover rather than another view. this way you'll be able to easily present and dismiss the view only on your demand. for more complete info than what i can explain here, please take a look at Paul Hudson's explanation in his website: https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-full-screen-modal-view-using-fullscreencover

Mahdi BM
  • 1,826
  • 13
  • 16