gpt4 book ai didi

ios - 如何在 UIAlertController 外部点击时关闭 UIAlertController?

转载 作者:IT王子 更新时间:2023-10-29 07:40:22 27 4
gpt4 key购买 nike

如何在 UIAlertController 外部点击时关闭 UIAlertController

我可以添加 UIAlertActionStyleCancel 样式的 UIAlertAction 来关闭 UIAlertController

但我想添加这样的功能,当用户点击 UIAlertController 外部时,UIAlertController 将关闭。怎么做?谢谢。

最佳答案

如果您的目标设备是 iOS > 9.3 并且使用 Swift 且 preferredStyle 是Alert,您可以使用如下代码片段:

func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:{
alert.view.superview?.userInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
})
}

func alertControllerBackgroundTapped()
{
self.dismissViewControllerAnimated(true, completion: nil)
}

使用 swift 3:

func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert)
self.present(alert, animated: true) {
alert.view.superview?.isUserInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
}
}

func alertControllerBackgroundTapped()
{
self.dismiss(animated: true, completion: nil)
}

关于ios - 如何在 UIAlertController 外部点击时关闭 UIAlertController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com