2

I have following code. Two big buttons at the top and a ScrollView with many buttons below:

struct ContentView: View {
    var body: some View {
        VStack (spacing: 0) {
            HStack (spacing: 0) {
                Button(action: {}, label: {Image("button_background")})
                Button(action: {}, label: {Image("button_background")})
            }
            ScrollView {
                LazyVGrid(columns: [GridItem(.adaptive(minimum: 64))], spacing: 0) {
                    ForEach(1...4, id: \.self) { number in
                        Button(action: {}, label: {
                            ZStack {
                                Color(.gray)
                                Text("A").foregroundColor(.white)
                            }
                        })
                    }
                }
            }
        }
    }
}

But when I click on one of the buttons in the first row in the ScrollView the button in the HStack above gets triggered. For example look at the photo below. I click on the "A" (the black dot, I wasn't able to show the cursor) but the big Button above gets triggered (and becomes gray). How can I fix that?

XCode Version 12.3

example

2
  • I copy pasted your code into clean SwiftUI project and everything works as expected, you click A, it triggers A, you click big button, it triggers big button. What is the problem? Commented Feb 14, 2021 at 11:56
  • I am also using exactly the code I pasted above. It depends on where I click. If I click the lower half of the "A" it works as expected but if I click the "upper half" it triggers the big button. Does it even work for you if you click the "A" as high as possible? XCode Version 12.3 Commented Feb 14, 2021 at 11:59

1 Answer 1

2

You can control the tappable area of a view using .contentShape().

In your case you can add .contentShape(Rectangle()) after HStack.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.