Home > database >  How can I make the full button clickable and not just the text? SwiftUI
How can I make the full button clickable and not just the text? SwiftUI

Time:11-05

                    Button("Login") {
                        authenticateUser(username: username, password: password)
                    }
                    .foregroundColor(Color.black)
                    .frame(width: 300, height: 50)
                    .background(Color.blue)
                    .cornerRadius(10)
                    
                    NavigationLink(destination: HomeView(), isActive: $showingLoginScreen) {
                        EmptyView()
                        
                    }

I am new to coding and have no clue how to fix this. I have to click on the text in order for the button to be activated.

CodePudding user response:

this is one way, make sure to read the apple documentation, check the different initializers there are for the button and other things.

Button {
  authenticateUser(username: username, password: password)
} label: {
  Text("Login")
      .foregroundColor(Color.black)
      .frame(width: 300, height: 50)
      .background(Color.blue)
      .cornerRadius(10)
}

Happy coding!

CodePudding user response:

struct ContentView: View {
    @State private var isTapped = false

    var body: some View {
        Button {
            isTapped.toggle()
        } label: {
            Text("Login")
                .foregroundColor(Color.black)
                .frame(width: 300, height: 50)
                .background(isTapped ? Color.blue : Color.green)
                .cornerRadius(10)
        }
    }
}
  • Related