覆盖在 UIWindow 上的 UIView 通过 SwiftUI 视图传递点击

问题描述 投票:0回答:1

我需要在

UIView
内容之上覆盖
SwiftUI
。我没有使用
UIViewController.present(_:animated:completion:)
,而是将其添加到
UIWindow
层次结构并应用约束(我需要它进行额外的自定义,这里并不重要)。

但是我遇到了一个问题。覆盖的

UIView
通过自身传递点击手势,并且底层
SwiftUI
View
被点击。您可以看到,连续的点击使屏幕变得越来越红——它们基本上插入了更多的子视图。

奇怪的是,拖动手势(例如滚动)不会通过。

这意味着什么?为什么是点击通过,而不是拖动?

struct ContentView: View {
    var body: some View {
        ScrollView(content: {
            Button("Click", action: overlayView)
        })
    }
    
    private func overlayView() {
        let overlay: UIView = .init()
        overlay.translatesAutoresizingMaskIntoConstraints = false
        overlay.isUserInteractionEnabled = true
        overlay.backgroundColor = .red.withAlphaComponent(0.1)
        
        let windowView: UIView = UIApplication.shared.activeWindow!.rootViewController!.view
        
        windowView.addSubview(overlay)
        
        NSLayoutConstraint.activate([
            overlay.leadingAnchor.constraint(equalTo: windowView.leadingAnchor),
            overlay.trailingAnchor.constraint(equalTo: windowView.trailingAnchor),
            overlay.topAnchor.constraint(equalTo: windowView.topAnchor),
            overlay.bottomAnchor.constraint(equalTo: windowView.bottomAnchor)
        ])
    }
}

extension UIApplication {
    var activeWindow: UIWindow? {
        connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .first { $0 is UIWindowScene }
            .flatMap { $0 as? UIWindowScene }?
            .windows
            .first { $0.isKeyWindow }
    }
}

从纯启动时不会出现同样的问题

UIKit
:

final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        
        let button: UIButton = .init()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(overlayView), for: .touchUpInside)
        button.setTitle("Click", for: .normal)
        button.setTitleColor(.systemBlue, for: .normal)
        
        view.addSubview(button)
        
        NSLayoutConstraint.activate([
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc private func overlayView() {
        let overlay: UIView = .init()
        overlay.translatesAutoresizingMaskIntoConstraints = false
        overlay.isUserInteractionEnabled = true
        overlay.backgroundColor = .red.withAlphaComponent(0.1)
        
        let windowView: UIView = UIApplication.shared.activeWindow!.rootViewController!.view
        
        windowView.addSubview(overlay)
        
        NSLayoutConstraint.activate([
            overlay.leadingAnchor.constraint(equalTo: windowView.leadingAnchor),
            overlay.trailingAnchor.constraint(equalTo: windowView.trailingAnchor),
            overlay.topAnchor.constraint(equalTo: windowView.topAnchor),
            overlay.bottomAnchor.constraint(equalTo: windowView.bottomAnchor)
        ])
    }
}

如果您能帮助实际切断点击手势,我将不胜感激。

swift swiftui uiview uiviewcontroller uikit
1个回答
0
投票

我找到了解决此问题的方法。我所做的是将最顶层呈现视图控制器的根视图上的

isUserInteractionEnabled
设置为
false
,而不是覆盖层。

© www.soinside.com 2019 - 2024. All rights reserved.