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, но ни один не работал в моем случае. Дайте мне знать, где я не прав.

Любая помощь будет оценена.

1 ответ

Решение

Ну, я нашел ответ. Я добавил селектор к кнопке в неправильном месте. Удалил этот код

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

от UIButton закрытие и добавлено viewDidLoad работал.

Другие вопросы по тегам