Let's say I have a SwiftUI view hierarchy that looks like this:
ZStack() {
ScrollView {
...
}
Text("Hello.")
}
The Text view blocks touch events from reaching the underlying ScrollView.
With UIKit, I'd use something like .isUserInteractionEnabled to control this, but I can't find any way to do this with SwiftUI.
I've tried adding a Gesture with a GestureMask of .none on the text view, but that doesn't seem to work.
I hope I'm missing something obvious here, because I need to put some status information on top of the scroll view.
There is a modifier on View to disable or enable user interaction:
disabled(_ disabled: Bool)
What about using the .allowsHitTesting()?
https://developer.apple.com/documentation/swiftui/image/3269586-allowshittesting
From my understanding it should pass the gesture to the view behind if it's disabled.
ZStack() {
ScrollView {
...
}
Text("Hello.").allowsHitTesting(false)
}
You want to make sure you fill your view with some color, except Clear color (you can always change opacity). I've also added blur to hide elements. Here's what worked for me:
SwiftUI:
ZStack{
SomeView().blur(radius: 12)
Rectangle()
.fill(Color.white.opacity(0))
.allowsHitTesting(false)
}
Another way to disable user interactions like scroll or button taps, but attach an action to user taps (for example a message to users that this feature is coming or behind a paywall):
SwiftUI:
VStack{
SomeView().blur(radius: 12)
}
.contentShape(Rectangle())
.onTapGesture {
print("No access!")
}
SwiftUI 2.0
ZStack {
// Your Stack to Disable User Interaction
}.disabled(showLoadingIndicator)
This is most likely a bug in SwiftUI, but the workaround for this problem was to remove a .border() that I had put on the Text view for bounds debugging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With