-2

This is the code of button

enter image description here

Why does it work if I click in the center and if I click on the side it doesn't? enter image description here

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39

1 Answers1

1

Because of clickable view frames are different..

You will understand better if you examine the code example below.

VStack {
                Button("Button1") {
                    print("Button clicked")
                }
                .frame(width: 350, height: 50, alignment: .center) //Button View frame
                .background(.purple)

                Button {
                    print("Button clicked")
                } label: {
                    //Clickable view frame
                    Text("Button2")
                        .frame(width: 300, height: 50, alignment: .center)
                        .background(.green)
                }
                .frame(width: 350, height: 50, alignment: .center)//Button View frame and You can remove this line if you understand..
                .background(.purple)
            }

Button1 is your coding style..

If you use to Button2 coding style, your problem will be solved. PS: Green background is clickable frame..

TnasuH
  • 25
  • 6