1

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)
        }
    }
}
NikaE
  • 614
  • 1
  • 8
  • 15

1 Answers1

2

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") }
                    
                }
            }

        }
        
        
        
    }
}
ios coder
  • 1
  • 4
  • 31
  • 91