Search code examples
buttonnavigationswiftuiviewcontroller

How to navigate between view controllers on a button action in swiftui?


I want to navigate between the login screen to sign up Screen with the help of button action.

   Button(action: {

                NavigationLink(destination:SignUpScene()){
                   Text("Show Detail View")
                                  }

                print("Join button tapped!!")
                   })

Solution

  • You can create navigation link and set properties to show it like button: Here is my code:

        var body: some View {
            NavigationView {
    
                //Button to navigate to another page
                NavigationLink(destination: AlertView()) {
                    Text("GO TO ALERT")
                        .padding(15)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .background(Color.red)
                        .foregroundColor(Color.white)
                        .cornerRadius(4)
                }
                .padding([.leading, .trailing], 30)
                .padding([.top], 21)
                .frame(height: 50)
            }
        }
    

    Here is how it looks:

    enter image description here

    Hope it helps..Good Luck :)