I'm working on a SwiftUI app where I have a ZStack that has a ViewController (wrapped in a UIViewRepresentable view) underneath and a semitransparent SwiftUI view on top. I need to be able to tap through the SwiftUI view to the ViewController, but this does not seem to work.
I've tried setting the SwiftUI view with .disabled(true) and .allowsHitTesting(false), but neither seem to resolve the issue. I've also tried using an overlay instead of a ZStack, but this did not help either.
How do I put a SwiftUI View above a ViewController and allow taps/scrolls/other gestures to pass through to the ViewController?
I made a sample project with 2 identical versions - it worked fine in the version with a SwiftUI view underneath, but did not work in the version with the ViewController underneath.
SwiftUI only version (tapping on the red view passes through correctly):
SwiftUI red view above a ViewController with a full screen button (tapping outside the red view works fine, but tapping on the red view does not pass through to the ViewController):
I've tried setting the SwiftUI view with .disabled(true) and .allowsHitTesting(false), but neither seem to resolve the issue. I've also tried using an overlay instead of a ZStack, but this did not help either.
How do I put a SwiftUI View above a ViewController and allow taps/scrolls/other gestures to pass through to the ViewController?
I made a sample project with 2 identical versions - it worked fine in the version with a SwiftUI view underneath, but did not work in the version with the ViewController underneath.
SwiftUI only version (tapping on the red view passes through correctly):
Code Block struct SwiftUIOnlyView: View { var body: some View { ZStack { Color.white.onTapGesture { print("tapped \(Date())") } Color.red .disabled(true) .opacity(0.20) .frame(width: 100, height: 100, alignment: .center) } } }
SwiftUI red view above a ViewController with a full screen button (tapping outside the red view works fine, but tapping on the red view does not pass through to the ViewController):
Code Block struct SwiftUIAndViewControllerView: View { var body: some View { ZStack { ViewControllerWrapperView() Color.red .disabled(true) .opacity(0.20) .frame(width: 100, height: 100, alignment: .center) } } } struct ViewControllerWrapperView: UIViewControllerRepresentable { func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerWrapperView>) -> ViewController { return ViewController() } func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<ViewControllerWrapperView>) { } } class ViewController: UIViewController { @IBAction func didTap(_ sender: Any) { print("tapped \(Date())") } }