我需要關閉一個AlertView時,用戶觸摸任何地方以外的任何地方。如何在觸摸它外面的任何地方時解除UIAlertView?
我知道你要調用
alert.dismissViewControllerAnimated(true, completion: nil)
駁回AlertView,但我要如何做到這一點,只有當用戶觸摸其他地方除了兩個按鈕是視圖的一部分嗎?
這是史蒂夫的建議後,我的代碼有:
self.presentViewController(alert, animated: true, completion:{
alert.view.superview?.userInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertClose(_:))))
})
注:預斯威夫特
presentViewController(alert, animated: true, completion: nil)
//Add gesture recognizer for alert ViewController when adding an event
let tapGesture = UITapGestureRecognizer(target: self, action: "alertClose:")
view.addGestureRecognizer(tapGesture)
//dismiss the alert if the user click anywhere except the buttons
func alertClose(gesture: UITapGestureRecognizer) {
alert.dismissViewControllerAnimated(true, completion: nil)
}
測試此您可以加入一個UITapGestureRecognizer查看,然後有行動電話報警。 dismissViewControllerAnimated? – Steve
也許你可以顯示自定義提醒。 – simalone