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



我有一个弹出窗口,我需要在UIWindow上显示。弹出窗口有 3 个按钮。弹出窗口已完美地添加到窗口中,但弹出窗口上的按钮对触摸没有响应。

这是我将弹出窗口添加到窗口的代码:

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的其他类似问题,但没有一个在我的情况下有效。让我知道我错在哪里。

任何帮助将不胜感激。

好吧,我找到了答案。我在错误的位置将选择器添加到按钮上。已删除此代码

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

UIButton关闭并添加到viewDidLoad工作。

最新更新