3

I want to have a login form on my app that is using SwiftUI. I have two buttons: a log in and a sign up. I want to be able to press the sign up button and have the new SwiftUI view that I made appear/replace the other one. Here is my code for my button:

        let signupButton = Color(red: 103.0/255.0, green: 104.0/255.0, blue: 255.0/255.0)

        let text : String

    var body: some View {

        Button(action: {
        }) {
            Text(text)
                .accentColor(.white)
          //      .frame(minWidth: 0, maxWidth: 300)
                .frame(width: 200)
                .padding()
                .background(signupButton)
                .cornerRadius(10)
                .padding()
        }
    }
}

And then for my login page:

    struct LoginPage: View {

    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

    let textFieldBackground = Color(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0)

    let textFieldBorder = Color(red: 112.0/255.0, green: 112.0/255.0, blue: 112.0/255.0)

    let signupButton = Color(red: 103.0/255.0, green: 104.0/255.0, blue: 255.0/255.0)

    var body: some View {
        NavigationView {
                Text("Don't have an account?")
                    .fontWeight(.light)
                NavigationLink(destination: SignupPage()) {
                    ActionButton(text: "sign up")
                }

                }
            .navigationBarTitle(Text("Signin")
            .foregroundColor(Color.black))
            }

                }
    }

The issue is, when I press the button, the signup page is not shown. I have an already designed page named SignupPage() and the reference is right. I don't know if this is a bug, or if I'm just doing this all wrong

1
  • Next time you post code, please make sure you format it right. There were several errors, including a missing line in what I suppose is your ActionButton view and also unbalanced brackets. Commented Aug 8, 2019 at 6:20

2 Answers 2

2

NavigationLink already behaves as a button, you do not need to use Button. If you did, you need to define an action for it. Here's a fix:

struct LoginPage: View {

    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

    let textFieldBackground = Color(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0)

    let textFieldBorder = Color(red: 112.0/255.0, green: 112.0/255.0, blue: 112.0/255.0)

    let signupButton = Color(red: 103.0/255.0, green: 104.0/255.0, blue: 255.0/255.0)

    var body: some View {
        NavigationView {
            VStack {
                Text("Don't have an account?")
                    .fontWeight(.light)
                NavigationLink(destination: SignupPage()) {
                    ActionButton(text: "sign up")
                }
            }
        }
        .navigationBarTitle(Text("Signin")
        .foregroundColor(Color.black))
    }

}

struct SignupPage: View {
    var body: some View {
        Text("Signup page placeholder")
    }
}

struct ActionButton: View {
    let signupButton = Color(red: 103.0/255.0, green: 104.0/255.0, blue: 255.0/255.0)

    let text : String

    var body: some View {

        Text(text)
            .accentColor(.white)
            //      .frame(minWidth: 0, maxWidth: 300)
            .frame(width: 200)
            .padding()
            .background(signupButton)
            .cornerRadius(10)
            .padding()
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can use a button, but you need to have a bool binded to isPresented on your NavigationLink, and when you click the button on his action you set this variable to true;
0

If you need to use a button and have some kind of behaviour before navigate to another view you can use the follow code:

struct FooView: View {
     @State private var presentView = false

     var body: some View {
         NavigationLink(destination: AnotherView(), isActive: self.$presentView) {
             Button(action: {
                 //do something here
                 self.presentView = true
             }) {
                 Text("Navigate!")
                     .frame(width: 100, height: 200)
             }
         }
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.