With this code, button is at the center of the view and message is logged only when tapping in the button
1. struct ContentView: View {
2. var body: some View {
3.
4. ZStack {
5. Button(action: {
6. print("Touched")
7. }) {
8. Image(systemName: "square.split.diagonal.2x2")
9. .font(.system(size: 24))
10. .foregroundColor(.black)
11. // .position(x: 12, y: 12)
12. }
13.
14. }
15. .frame(width: 300, height: 300)
16. .background(Color.yellow)
17. }
18. }
But if I uncomment line 11, the message is logged on tap anywhere in the view.
How is it position() changes the behaviour ?
I finally found a solution. Enclose Button in a HStack and apply position modifier to HStack and not directly to image.
struct ContentView: View {
var body: some View {
ZStack {
HStack { // <<-- ENCLOSE IN HStack
Button(action: {
print("Touched")
}) {
Image(systemName: "square.split.diagonal.2x2")
.font(.system(size: 24))
.foregroundColor(.black)
// .position(x: 12, y: 12) // <<-- NOT HERE
}
}
.position(x: 12, y: 12) // <<-- MOVED HERE
}
.frame(width: 300, height: 300)
.background(Color.yellow)
}
}
What is the reason why ?