Home > Mobile >  SwiftUI Button touch area is abnormal
SwiftUI Button touch area is abnormal

Time:07-15

I created SwiftUI Button and its touch area is slightly strange. The touch area extends the label of the button.

This is my code.

struct TestView: View {
    var body: some View {
        HStack(spacing: 0) {
            Spacer()
        
            Button {
                print("aaaaaa")
            } label: {
                HStack(spacing: 0) {
                    Spacer()
                }
                .frame(width: 50.0, height: 50.0)
                .background(Color.yellow)
            }
        
            Spacer()
        }
        .frame(height: 50.0)
        .background(Color.red)
    }
}

And I'll attach the result in simulator.

The button area is filled with yellow color. But I could click the button outside of yellow color.

The same thing happens in real device too.

How is this possible?

Touch area

CodePudding user response:

This is a completely normal behavior of Button/onTapGesture in SwiftUI. Its touch area is slightly bigger than its border size.

This is not a bug or glitch.

  • Related