当用户触摸警报时,如何防止我的 SDCAlertview 关闭?

Posted

技术标签:

【中文标题】当用户触摸警报时,如何防止我的 SDCAlertview 关闭?【英文标题】:How do i prevent my SDCAlertview from dismissing when the user touches around the alert? 【发布时间】:2017-11-09 06:48:50 【问题描述】:

如何将dismissOnOutsideTapped 设置为false?我试图阻止用户在点击 Ok 按钮之前关闭 AlertController

myView.translatesAutoresizingMaskIntoConstraints = false
myView.heightAnchor.constraint(equalToConstant: 100).isActive = true

    let attributedString = NSAttributedString(string: title, attributes: [
        NSAttributedStringKey.foregroundColor : #colorLiteral(red: 0.8226075768, green: 0.2890471816, blue: 0.2390623093, alpha: 1)
        ])
    let alert = AlertController(title: "", message: "")
    alert.setValue(attributedString, forKey: "attributedTitle")


let OkAction: AlertAction = AlertAction(title: "OK", style: .preferred)  (action) in
   MyCode......

alert.addAction(OkAction)
alert.contentView.addSubview(myView)
myView.leftAnchor.constraint(equalTo: alert.contentView.leftAnchor).isActive = true
myView.rightAnchor.constraint(equalTo: alert.contentView.rightAnchor).isActive = true
myView.centerXAnchor.constraint(equalTo: alert.contentView.centerXAnchor).isActive = true
myView.topAnchor.constraint(equalTo:alert.contentView.topAnchor, constant: -25).isActive = true
content.bottomAnchor.constraint(equalTo: alert.contentView.bottomAnchor).isActive = true



alert.view.tintColor = #colorLiteral(red: 0.8226075768, green: 0.2890471816, blue: 0.2390623093, alpha: 1)
alert.present()

【问题讨论】:

【参考方案1】:

您可以使用AlertBehaviors 禁用外部触摸。像这样。

let alert = AlertController(title: "", message: "")
alert.behaviors = AlertBehaviors.init(rawValue: 0) 

详情请参阅 github docs 中的 AlertBehaviors

【讨论】:

以上是关于当用户触摸警报时,如何防止我的 SDCAlertview 关闭?的主要内容,如果未能解决你的问题,请参考以下文章

在警报中选择选项时,如何防止我的文本字段成为第一响应者?

如何在警报视图中更改文本颜色和配置文本触摸操作?

防止在外部触摸时解除 BottomSheetDialogFragment

如何防止多个 UIAlertView 堆叠?

android:当用户触摸片段外部时,我如何关闭片段?

如何防止键盘出现在自定义警报视图上