79113593

Date: 2024-10-22 10:32:46
Score: 1
Natty:
Report link

View hierarchy example

You should add gesture recognizers to the presentation content view.

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

present(alertController, animated: true, completion: {
    let backgroundTapDismissGesture = UITapGestureRecognizer(target: self, action: #selector(didTapDismissGesture(_:)))

    let transitionBGView = alertController.presentationController?.containerView?.subviews.first
    transitionBGView?.isUserInteractionEnabled = true
    transitionBGView?.addGestureRecognizer(backgroundTapDismissGesture)
})

...

@objc func didTapDismissGesture(_ sender: UITapGestureRecognizer) {
    alertController.dismiss(animated: true)
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Vladyslav Yermakov