Zaid Raza
Zaid Raza

Reputation: 65

Tappable area of Button and Image in SwiftUI


    Button(action: {
       print("Round Action")
       }) {
       Text("Press")
       .frame(width: 50, height: 50)
       .foregroundColor(Color.black)
       .background(Color.red)
       .clipShape(Circle())
       }
       .background(Color.blue)

I want the actual button to be tappable and not the square blue frame around it, also is there any way we can just get rid of that frame but could still be able to size that button?

Button Image

Upvotes: 2

Views: 1677

Answers (1)

pawello2222
pawello2222

Reputation: 54651

You need to set .contentShape for your Button, by default it's a Rectangle:

Button(action: {
    print("Round Action")
}) {
    Text("Press")
        .frame(width: 150, height: 150)
        .foregroundColor(Color.black)
        .background(Color.red)
        .clipShape(Circle())
}
.contentShape(Circle()) // <- here
.background(Color.blue)

is there any way we can just get rid of that frame but could still be able to size that button?

You can try to use padding or scaleEffect but frame is the recommended way of changing the width and height of a view.

Upvotes: 3

Related Questions