如何在UIAlertController外部点击时关闭UIAlertController?

lei*_*701 43 uialertview ios uialertcontroller

如何UIAlertController在外面敲击时解雇UIAlertController

我可以添加一种UIAlertAction风格UIAlertActionStyleCancel来解雇UIAlertController.

但我想补充一点,当外界使用者开关功能UIAlertControllerUIAlertController将开除.怎么做?谢谢.

Viv*_*dav 42

使用样式添加单独的取消操作UIAlertActionStyleCancel.因此,当用户点击外部时,您将获得回调.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
 // Called when user taps outside
}]];
Run Code Online (Sandbox Code Playgroud)

-

  • 这不适用于Alert样式 (9认同)
  • 您的 Swift 5.0 版本只是添加了一个“取消”按钮,您可以为其分配代码,但点击警报外部不会触发任何内容,您必须单击该按钮。 (6认同)

cha*_*yWu 40

如果您要定位iOS> 9.3并使用Swift并且preferredStyle为Alert的设备,则可以使用以下代码段:

func showAlertBtnClicked(sender: UIButton) {
    let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
    self.presentViewController(alert, animated: true, completion:{
        alert.view.superview?.userInteractionEnabled = true
        alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
    })
}

func alertControllerBackgroundTapped()
{
    self.dismissViewControllerAnimated(true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

随着迅捷3:

func showAlertBtnClicked(sender: UIButton) {
    let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert)
    self.present(alert, animated: true) {
        alert.view.superview?.isUserInteractionEnabled = true
        alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
    }
}

func alertControllerBackgroundTapped()
{
    self.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

  • 从iOS 9.3开始,您必须将手势识别器添加到superview的第二个子视图(`alert.view.superview.subviews [1]`)而不是superview本身.如文档中所述,UIAlertController的视图层次结构是私有的,因此没有保证它在将来的iOS版本中不会更改. (14认同)
  • 不确定这是否是 Swift 5 中的更改,但您必须将 `@objc` 添加到 `alertControllerBackgroundTapped` 函数中,否则 Xcode 会解释。除此之外,Swift 3 版本也适用于 Swift 5,谢谢。 (2认同)

Kaz*_*mun 17

Swift,Xcode 9

使用取消按钮关闭AlertController

提供行动的alertController那里UIAlertAction的风格.cancel

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
Run Code Online (Sandbox Code Playgroud)

使用此方法当用户点击取消操作按钮以及alertController外部时,alertController将被解除.

如果您不希望用户在alertController外部触摸后解除alertController,请在当前方法的完成闭包中禁用alertController的第一个子视图的用户交互.

self.present(alertController, animated: true) {
     alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
    }
Run Code Online (Sandbox Code Playgroud)

在Controller视图外部的touchup上关闭AlertController

如果您不想在控制器视图中使用取消按钮,并且想要在控制器视图外部进行用户修改时关闭控制器,请执行此操作

self.present(alertController, animated: true) {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissAlertController))
        alertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
}

@objc func dismissAlertController(){
    self.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)


Kev*_*ado 7

如果您使用的是Swift:

使用addAction(_:)和添加操作style:UIAlertActionStyle.Cancel.

当点击按钮或框架外时,将调用`处理程序.

var alertVC = UIAlertController(...) // initialize your Alert View Controller

        alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
            (alertAction: UIAlertAction!) in
            alertVC.dismissViewControllerAnimated(true, completion: nil)
        }))
Run Code Online (Sandbox Code Playgroud)

Objective-C:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...];


[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
   [alertVC dismissViewControllerAnimated:YES completion:nil];
}]];
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法在菜单外面按下来关闭它,但没有"取消"按钮? (21认同)

小智 5

斯威夫特 4:

当用户在使用 UIAlertController 创建的操作表之外点击时关闭操作表

代码片段:

// Declare Action Sheet reference
var actionSheet: UIAlertController!

// Init and Show Action Sheet
func showActionSheetClicked(sender: UIButton) {

    // Init Action Sheet
    actionSheet = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)

    self.present(actionSheet, animated: true) {
        // Enabling Interaction for Transperent Full Screen Overlay
        self.actionSheet.view.superview?.subviews.first?.isUserInteractionEnabled = true

        // Adding Tap Gesture to Overlay
        self.actionSheet.view.superview?.subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.actionSheetBackgroundTapped)))
    }
}

// To dismiss Action Sheet on Tap
@objc func actionSheetBackgroundTapped() {
    self.actionSheet.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)