无法在 UIWindow 上添加的 UIView 上禁用 UserInteraction

问题描述

所以问题来了。我创建了一个自定义加载器,它在中间显示一个基本的“加载”文本,背景变暗。 HUD 显示得很好,但尽管将 isUserInteractionEnabled 设置为 false,但我仍然可以单击添加了 HUD 的 UI。代码如下:

public class Loader {
    public static let shared = Loader()
    
    private(set) var containerView = UIView()
    private(set) var lblText = UILabel()
    
    private init() {
        self.containerView.frame = UIScreen.main.bounds
        self.containerView.backgroundColor = UIColor(red: 0/255,green: 0/255,blue: 0/255,alpha: 0.5)
        self.containerView.isUserInteractionEnabled = false
        
        lblText.font = UIFont.boldSystemFont(ofSize: 30)
        lblText.textAlignment = .center
        lblText.textColor = .white
        lblText.text = "Loading..."
        containerView.addSubview(lblText)
        
        lblText.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint(item: lblText,attribute: NSLayoutConstraint.Attribute.centerX,relatedBy: NSLayoutConstraint.Relation.equal,toItem: containerView,multiplier: 1,constant: 0).isActive = true
        NSLayoutConstraint(item: lblText,attribute: NSLayoutConstraint.Attribute.centerY,constant: 0).isActive = true
        
        NSLayoutConstraint(item: lblText,attribute: NSLayoutConstraint.Attribute.width,toItem: nil,attribute: NSLayoutConstraint.Attribute.notAnAttribute,constant: 275).isActive = true
        lblText.heightAnchor.constraint(equalTo: lblText.widthAnchor,multiplier: 181/375).isActive = true
    }
    
   
    public func show() {
        DispatchQueue.main.async( execute: {
            if let found = UIApplication.key?.subviews.contains(where: {$0 == self.containerView}),found {
                self.containerView.superview?.bringSubviewToFront(self.containerView)
            }
            else {
                UIApplication.key?.addSubview(self.containerView)
            }
        })
    }
    
    @objc public func hide() {
        DispatchQueue.main.async( execute: {
            self.containerView.removeFromSuperview()
        })
    }
}

extension UIApplication {
    static var key: UIWindow? {
        if #available(iOS 13,*) {
            return UIApplication.shared.connectedScenes
                .filter({$0.activationState == .foregroundActive})
                .map({$0 as? UIWindowScene})
                .compactMap({$0})
                .first?.windows
                .filter({$0.isKeyWindow}).first
        } else {
            // do lower version specific window setup
            return UIApplication.shared.keyWindow
        }
    }
}

用法:Loader.shared.show()Loader.shared.hide()

代码有什么问题?

解决方法

   Maybe when you show the loader you can change the main view isUserInteractionEnabled to false and after you will finish the loading process change it again to true?
    
             DispatchQueue.main.async {
               Loader.shared.show()
               self.isUserInteractionEnabled = false
            }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...