How to disable user interaction on SwiftUI view? How to disable user interaction on SwiftUI view? swift swift

How to disable user interaction on SwiftUI view?


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


There is a modifier on View to disable or enable user interaction:

disabled(_ disabled: Bool)


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