我有一个UIAlertController
,其样式为ActionSheet
。
我向它添加了一个取消
操作。
问题是,由于某种原因,当用户在 ActionSheet
框外点击时 - 警报被取消。
如何禁用此行为?我希望用户直接按下取消按钮。
最佳答案
swift 4/Xcode 11
嗨。要禁用用户交互(例如:点击取消按钮以外的任何地方),请在您的警报函数中调用 present 方法并将其设置为完成关闭,如下代码所示。
present(alertController, animated: true) {
alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
}
整个例子:
func alertMe() {
let alertController = UIAlertController(title: "Tapping Test", message: "User Interaction on the view is disabled", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "cancel", style: UIAlertActionStyle.cancel) { UIAlertAction in
// do something/call someone/or nothing :)
NSLog("cancel Button is Pressed")
}
alertController.addAction(cancelAction)
present(alertController, animated: true){
alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
}
}
关于ios - 禁用 UIAlertController 框架外的触摸以取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50721687/