2

I have a problem, after I register a new user, the app goes back to the main menu (using a NavigationLink), then if I choose one of the options in the menu, in the next view I have 2 navigation bars.

Im working with NavigationView in the MainView.

enter image description here

struct ContentView: View {
var body: some View {
    NavigationView {
        MainView()
    }
}

}

struct MainView: View {
var body: some View {
        VStack {
            NavigationLink(
                destination: AuthView(),
                label: {
                    Text("Sesion")
                })
            
        }
        
}

}

struct AuthView: View {

@State private var email: String = ""
@State private var password: String = ""

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

var body: some View {
        VStack() {
          
                NavigationLink(
                    destination: RegisterView(),
                    label: {
                        Text("Registrarse")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                            .frame(width: 200, height: 60)
                            .background(Color.green)
                            .cornerRadius(15.0)
                    })
            }
        }
        .padding()
        .navigationBarTitle("Autenticacion")
        .navigationBarTitleDisplayMode(.inline)
    

}

struct RegisterView: View {
@ObservedObject var registerController = RegisterController()

@State private var showingRegistrationAlert = false

@State var navigateNext = false

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

var body: some View {
        VStack {
            VStack(alignment: .leading) {
                
            Spacer()
            
            NavigationLink(destination:  ContentView(), isActive:$navigateNext) {
                                Text("")
            }
                Button(action: { if registerController.validateRegistration() {
                    showingRegistrationAlert.toggle()
                }}) {
                    Text("Enviar registro")
                }
                .alert(isPresented: $showingRegistrationAlert) {
                    Alert(title: Text("Se registro correctamente"), dismissButton: .default(Text("Ok"), action: {
                        navigateNext.toggle()
                    }))
                
            }
        .padding()
        .navigationBarTitle(Text("Registracion"), displayMode: .inline)
    
}

}

I tried to put the less code possible, sorry if it's a lot.

2
  • this is very common issue, and 1000 time answered! have you searched in older questions? Commented Mar 1, 2021 at 15:13
  • Yes, but I can't solve it, ty. Commented Mar 1, 2021 at 15:44

2 Answers 2

2

You are adding second NavigationView in RegisterView. That is the issue! because of using ContentView, for solving use MainView().

 struct RegisterView: View {
@ObservedObject var registerController = RegisterController()

@State private var showingRegistrationAlert = false

@State var navigateNext = false

let lightGreyColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

var body: some View {
        VStack {
            VStack(alignment: .leading) {
                
            Spacer()
            
            NavigationLink(destination: MainView(), isActive:$navigateNext) {             // <<: here
                                Text("")
            }
                Button(action: { if registerController.validateRegistration() {
                    showingRegistrationAlert.toggle()
                }}) {
                    Text("Enviar registro")
                }
                .alert(isPresented: $showingRegistrationAlert) {
                    Alert(title: Text("Se registro correctamente"), dismissButton: .default(Text("Ok"), action: {
                        navigateNext.toggle()
                    }))
                
            }
        .padding()
        .navigationBarTitle(Text("Registracion"), displayMode: .inline)
    
}
}
Sign up to request clarification or add additional context in comments.

Comments

2

You only want to have NavigationView be on the TOP view in your hierarchy - don't use it on any of the child views, even if presented by a NavigationLink.

Edit: If you're already doing this, please post a code sample and I can look in more detail.

Another option I favour is wrapping SwiftUI views in a UIHostingController and using UIKit for navigation - since NavigationLinks have been buggy since release.

1 Comment

Yes, im already doing that, now im gonna post the sample, ty.

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.