在 UIWindow 的子视图中添加时,UIButton 不响应点击

UIButton does not respond to tap when added inside a subview on UIWindow

我有一个弹出窗口需要在 UIWindow 上显示。弹出窗口有 3 个按钮。弹出窗口完美添加到 window 但弹出窗口上的按钮不响应触摸。

这是我的代码,用于向 window 添加弹出窗口:

let windowCount = UIApplication.shared.windows.count
UIApplication.shared.windows[windowCount-1].insertSubview(blurView, at: (UIApplication.shared.windows.last?.subviews.count)!)

要弹出的代码

func setUpHelpMenu(){

            blurView.addSubview(backView)
            _ = backView.anchorPoints(left: blurView.leftAnchor, bottom: blurView.bottomAnchor, right: blurView.rightAnchor,  leftConstant: 0, bottomConstant: 10, rightConstant: 0, widthConstant: blurView.frame.width, heightConstant: 170/812 * blurView.frame.size.height)

            backView.addSubview(resendCode)
            backView.addSubview(editPhoneNumber)
            backView.addSubview(cancel)

            _ = resendCode.anchorPoints(backView.topAnchor, centerX: backView.centerXAnchor, widthConstant: backView.frame.size.width, heightConstant: backView.frame.size.height / 3)
            _ = editPhoneNumber.anchorPoints(resendCode.bottomAnchor, centerX: backView.centerXAnchor, widthConstant: backView.frame.size.width, heightConstant: backView.frame.size.height / 3)
            _ = cancel.anchorPoints(editPhoneNumber.bottomAnchor, centerX: backView.centerXAnchor, widthConstant: backView.frame.size.width, heightConstant: backView.frame.size.height / 3 - 10)
        }

取消按钮设计代码:

let cancel: UIButton = {
        let button = UIButton()
        button.buttonTitleLabelWith(titleEdgeInsets: .zero, title: String(string: "Cancel") as NSString, lineBreakMode: .byWordWrapping, font: UIFont.systemFont(ofSize: 25/1024 * UIScreen.main.bounds.height), textColor:  UIColor.blue.withAlphaComponent(0.80))
        button.isUserInteractionEnabled = true
        button.backgroundColor = .blue
        button.addTarget(self, action: #selector(tapPopUpButtons), for: .touchUpInside)
        return button
    }()

我已经检查过关于 SO 的其他类似问题,但 none 在我的案例中有效。让我知道我哪里错了。

如有任何帮助,我们将不胜感激。

嗯,我找到了答案。我将选择器添加到错误位置的按钮。删除了这段代码

button.addTarget(self, action: #selector(tapPopUpButtons), for: .touchUpInside)

UIButton 关闭并添加到 viewDidLoad 有效。