在UIWindow上的子视图中添加UIButton时,UIButton不响应tap

时间:2018-06-18 08:52:06

标签: ios swift uiview uiwindow touch-up-inside

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

以下是我向窗口添加弹出窗口的代码:

equation

弹出的代码

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)
        }

我在SO上检查了其他类似的问题,但没有一个在我的案例中有用。让我知道我错在哪里。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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

UIButton结束并添加到viewDidLoad开始有效。