Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Tap Gesture does not work inside ScrollView

Tags:

swiftui

I am drawing boxes and I would like to tap on them. However, Tap gesture does not work if I put code inside ScrollView. If I remove GeometryReaer the code works, however I need it (although I do not use it in this simple example). Any ideas how to make code work with ScrollView and GeometryReader?

enter image description here

struct ContentView: View {
    var body: some View {
        // REMOVING ScrollView makes TapGesture WORK
        ScrollView {
            WorksGridView()
        }
    }
}

struct WorksGridView: View {
    private var items = 5
    
    var body: some View {
        GeometryReader { geometry in
            VStack(spacing: 0) {
                ForEach(0..<self.items) { index in
                    GridInventoryItem()
                        .onTapGesture {
                            print("I'm not appearing!!!")
                    }
                }
            }
        }
    }
}

struct GridInventoryItem: View {
    var body: some View {
        Rectangle()
            .padding()
            .frame(width: 150, height:  150)
    }
}
like image 500
Markon Avatar asked Oct 25 '25 13:10

Markon


1 Answers

Move the ScrollView inside of the Geometry Reader and it works: Tested on iOS 13.5

struct ContentView: View {
    var body: some View {
        // REMOVING ScrollView makes TapGesture WORK
        
        WorksGridView()
        
    }
}

struct WorksGridView: View {
    private var items = 5
    
    var body: some View {
        GeometryReader { geometry in
            ScrollView{
                VStack(spacing: 0) {
                    ForEach(0..<self.items) { index in
                        GridInventoryItem()
                            .onTapGesture {
                                print("I'm not appearing!!!")
                            }
                    }
                }
            }
        }
    }
}

struct GridInventoryItem: View {
    var body: some View {
        Rectangle()
            .padding()
            .frame(width: 150, height:  150)
    }
}
like image 162
Simon Avatar answered Oct 27 '25 02:10

Simon