1
votes

Tap gesture on green does not work. Does anyone have a solution for this?

ZStack {
    Color.green
        .onTapGesture {
            print("green")
        }
    ScrollView {
        VStack {
            Spacer(minLength: 500)
            Color.red
                .onTapGesture {
                    print("red")
                }
                .frame(height: 800)
        }
    }
}
1

1 Answers

1
votes

The thing you want is impossible because ScrollView is over the green View, but there is way like this:

    struct ContentView: View {
    
    func greenFunction() { print("green") }
    
    var body: some View {

        ZStack {
            
            Color.green.onTapGesture { greenFunction() }

            ScrollView {

                VStack(spacing: 0) {
                    
                    Color.white.opacity(0.01).frame(height: 500).onTapGesture { greenFunction() }
                    
                    Color.red.frame(height: 800).onTapGesture { print("red") }
                    
                }
            }

        }
        
        
        
    }
}