skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. Button {
      showLoginView = true
    } label: {
     Text("Login")
    }
    NavigationLink("", destination:  LoginView(), isActive: $showLoginView)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search