【问题标题】:UIButton does not respond to tap when added inside a subview on UIWindow在 UIWindow 的子视图中添加时,UIButton 不响应点击
【发布时间】:2018-06-18 08:52:05
【问题描述】:

我需要在 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 的其他类似问题,但没有一个对我有用。让我知道我错在哪里。

任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过将 backview 的 clipsToBounds 属性设置为 true 以查看您的按钮是否正在剪切?
  • 关键窗口首先捕获用户交互。因此,如果您已将多个窗口添加到您的应用程序并希望弹出出现在特定窗口上,请使用 makeKeyAndVisible 制作该窗口键窗口,否则使用 UIApplication.shared.keyWindow 添加弹出视图
  • @SandeepBhandari,你的意思是我应该制作这个窗口UIApplication.shared.windows[windowCount-1]keyAndVisible?
  • @sushil-sharma :您的应用程序中有多个窗口吗?您添加的自定义?您始终可以使用isKeyWindow 来查找您的窗口是否是关键。例如:if ! UIApplication.shared.windows[windowCount-1]. isKeyWindow { //then make your window key}

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


【解决方案1】:

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

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

UIButton 关闭并添加到viewDidLoad 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多