【问题标题】:Failed to disable UserInteraction on UIView added on UIWindow无法在 UIWindow 上添加的 UIView 上禁用 UserInteraction
【发布时间】:2021-06-14 21:00:39
【问题描述】:

所以这就是问题所在。我创建了一个自定义加载器,它在中间显示一个基本的“加载”文本,背景变暗。 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, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: lblText, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: containerView, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0).isActive = true
        
        NSLayoutConstraint(item: lblText, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, 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()

代码有什么问题?

【问题讨论】:

    标签: ios swift uiview uiwindow isuserinteractionenabled


    【解决方案1】:
       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
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      相关资源
      最近更新 更多