禁用 UIAlertController 框架外的触摸以取消



我有一个风格为ActionSheetUIAlertController

我向其添加了cancel操作。

问题是,由于某种原因,当用户点击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
}
}

最新更新