Reputation: 161
In a SwiftUI macOS app, I need a ScrollView
that allows both horizontal and vertical scrolling and contains interactive subviews.
Unfortunately, there seems to be a bug where onTapGesture
events get triggered based on incorrect coordinates, at least when the contents are larger than the scroll view. The click targets are higher up than the visible elements, apparently because they're placed in the window's coordinate space rather than the content area's (the offset matches the title bar height and is greater when a toolbar is added). The problem goes away after you scroll, but comes back if you horizontally resize the window.
So in the following example, you'll have to click just above the rectangle to make it toggle its colour, until you scroll and it starts behaving correctly:
struct ContentView: View {
@State var bool = false
var body: some View {
ScrollView([.vertical, .horizontal]) {
Rectangle()
.fill(bool ? .black : .gray)
.onTapGesture {
bool.toggle()
}
.frame(width: 4000, height: 30)
}
}
}
I'm reporting this as a bug to Apple, but I'm wondering if anyone knows a workaround. I don't want to use nested scroll views (one vertical, one horizontal) because the scroll indicator of the inner one could get hidden by the outer one.
Upvotes: 2
Views: 113