Home > database >  How To Switch To A New View On Simple Button Click?
How To Switch To A New View On Simple Button Click?

Time:12-29

I am simply trying to open up a new view when the user clicks a button. What is the most simple, and easiest way to do this using Swift API?

  //Login Button
            Button(
                action:
                {
                // Open Signup Screen
                    Signup();
                },
                label:
                {
                // How the button looks li
                Text("Create New Account")
                .foregroundColor(.white)
                .font(.title)
                }
                )
                .frame(maxWidth: .infinity, alignment: .center)
                .background(Color(red: 0.22, green: 0.655, blue: 0.02))
                .cornerRadius(8)
                .padding(.horizontal, metrics.size.width*0.10)
            
        }

Thanks.

Isaiah Thompson

CodePudding user response:

The best way would be to use a NavigationLink and wrapping the content inside a NavigationView.

The NavigationView is used to represent the view hierarchy.

For instance:

// Wrapper for LogIn
struct ContentView: View {
    var body: some View {
      NavigationView { LogIn() }
    }
}

// LogIn
struct LogIn: View {
  var body: some View {
    VStack {
      // Fields for log in

      NavigationLink(destination: SignUp()) {
        Text("Create New Account")
          .foregroundColor(.white)
          .font(.title)
          .frame(maxWidth: .infinity, alignment: .center)
          .background(Color(red: 0.22, green: 0.655, blue: 0.02))
          .cornerRadius(8)
          .padding(.horizontal, metrics.size.width*0.10)
      }
    }
  }
}

You can find more info in the official docs:

Also [Hacking With Swift] (https://www.hackingwithswift.com/quick-start/swiftui/displaying-a-detail-screen-with-navigationlink) is a great resource for SwiftUI

  • Related