1

I want to dismiss my UIAlertViewController by tapping outside of the UIAlertViewController, on tapping on black screen space. I've tried this:

self.presentViewController(alertViewController, animated: true, completion:{
        alertViewController.view.superview?.userInteractionEnabled = true
        alertViewController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertClose(_:))))
    })

but it just closes if I tapped on UIAlertViewController. But I want outside, where half-blacked screen tapped.

Is it possible?

UPDATE

@IBAction func shareButtonTapped(sender: UIButton) {
    let alertViewController = UIAlertController(title: "Share on social networks", message: "Where do you want to share?", preferredStyle: .ActionSheet)

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissAlertView:")
    self.view.addGestureRecognizer(tapGestureRecognizer)

    let facebookAction = UIAlertAction(title: "Facebook", style: .Default) { (alert: UIAlertAction) -> Void in

    }

    let twitterAction = UIAlertAction(title: "Twitter", style: .Default) { (alert: UIAlertAction) -> Void in

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .Destructive) { (alert: UIAlertAction) -> Void in

    }

    alertViewController.addAction(facebookAction)
    alertViewController.addAction(twitterAction)
    alertViewController.addAction(cancelAction)

    self.presentViewController(alertViewController, animated: true, completion:{
        alertViewController.view.superview?.userInteractionEnabled = true
        alertViewController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertClose(_:))))
    })
}

AlertClose function:

func alertClose(gesture: UITapGestureRecognizer) {
    self.dismissViewControllerAnimated(true, completion: nil)
}
John
  • 183
  • 1
  • 3
  • 11
  • Post your `alertClose` function. – Code Jun 22 '16 at 12:54
  • I presume you mean UIAlertController? You may need to subclass UIAlertController, set the GestureRecognizer delegate to self, so you can then override shouldReceiveTouches:. This way you can return false when the touch is over the main window. This should give you the desired effect. – aronspring Jun 22 '16 at 12:58
  • @Code done! I've added other parts of my code – John Jun 22 '16 at 12:59
  • Already ask question with answer [HERE.](http://stackoverflow.com/questions/30075832/how-to-dismiss-uialertcontroller-when-tap-outside-the-uialertcontroller?rq=1) – Mitul Marsoniya Jun 22 '16 at 13:03
  • @mitulmarsonia those answer did not help me =/ – John Jun 22 '16 at 13:25

2 Answers2

2

If you are using ActionSheet style there is no need to do this. Because when you tap on half-blacked screen view controller automatically will be dismissed.

But if you want to use with Alert style do following (without: alertViewController.view.superview?.userInteractionEnabled = true) :

        self.presentViewController(alertViewController, animated: true, completion: {

            let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.alertClose(_:)))
            alertViewController?.view.superview?.addGestureRecognizer(tapGestureRecognizer)


        })
1
  1. Try to add UITapGestureRecognizer to your UIWindow class like view.window.addGestureRecognizer()

  2. implement UIGestureRecognizerDelegate method shouldReceiveTouch and check your tapped view

Hope this help

iSashok
  • 2,326
  • 13
  • 21