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

标签 ios iphone swift cocoa-touch uialertcontroller

我有一个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/

相关文章:

ios - SwiftUI MVVM 的参数 JSONDecoder URLSession 缺少参数

ios - iPhone : Daily local notifications

swift - 结构变异函数调用同一结构的另一个实例中的函数

ios - 如何修复 GoogleUtilities Swift 的重复错误?

ios - UIPickerView 没有选择正确的行

iphone - UIScrollview 大内容尺寸

ios - Xcode 不生成 dSYM 文件

iphone - 自动调整 UIToolbar 横向高度

iphone - 在 Objective C 中绘制图像和文本的最简单方法是什么?

ios - Mantle - 将字符串转换为枚举值