1

I'm having an issue with SwiftUI where only the top part of the button is clickable, while the bottom part is not clickable at all.

Here is my code snippet. Does anyone know how I can fix this?

struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            ScrollView(.horizontal) {
                HStack {
                    Button(action: {}, label: {
                        ZStack {
                            Color.green
                            Text("Button")
                                .padding(8)
                                .contentShape(Rectangle())
                        }
                    })
                }
            }
            .background(Color.blue)
            .frame(height: 40)
            
            TabView {
                ZStack {
                    Color.yellow
                    Text("my view")
                }
            }
        }
    }
}

I have tried to change the VStack to ZStack, or introduce the Zindex but it isn't helped. The issue is happening with the combination of ScrollView + TabView.

1
  • 1
    The default TabView is designed to be at the top of the hierarchy. It isn't designed to be a part of a VStack Commented Sep 3, 2024 at 13:02

2 Answers 2

3

I don't work with SwiftUI (only played with it a bit) but after some quick searching...

It appears TabView has an extended "hit-test" area at the top.

For your layout, add a .contentShape modifier:

        TabView {
            ZStack {
                Color.yellow
                Text("my view")
            }
        }
        .contentShape(Rectangle())    // <-- should fix the issue
Sign up to request clarification or add additional context in comments.

1 Comment

It did solve the issue!!! Great Thanks Sir!
0

The issue you're encountering is likely due to the way the ScrollView and Button are structured in your layout. Specifically, the problem may arise because the Button is inside a ZStack, and its hit area might be constrained or overlapped by other views.

struct ContentView: View {
var body: some View {
    VStack(spacing: 0) {
        ScrollView(.horizontal) {
            HStack {
                Button(action: {
                    print("Button clicked")
                }) {
                    Text("Button")
                        .foregroundColor(.white)
                        .padding(8)
                        .background(Color.green)
                        .cornerRadius(8)
                        .contentShape(Rectangle()) // Ensure the entire area is clickable
                }
                .frame(height: 40) // Set a clear frame for the button
            }
        }
        .background(Color.blue)
        .frame(height: 40)
        
        TabView {
            ZStack {
                Color.yellow
                Text("my view")
            }
        }
    }
}

}

1 Comment

I have tried your code but still the bottom part of the button is not clickable.

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.