To show alert I use SDCAlertView(clone of UIAlertView). I want to dismiss alert by tap on screen like UIActionSheet.
Asked
Active
Viewed 170 times
2 Answers
1
Unlike UIAlertView
, SDCAlertView
is added as a view to the view hierarchy. That means that you can simply add a tap gesture recognizer to SDCAlertView
's superview, which calls [SDCAlertView dismissWithClickedButtonIndex:animated:]
.

Scott Berrevoets
- 16,921
- 6
- 59
- 80
-
How to do it now with SDCAlertController? – ChikabuZ Nov 01 '15 at 16:27
-
You could try adding it to the view controller's view, but that may not be exactly what you want – Scott Berrevoets Nov 01 '15 at 19:53
0
I found one way how do do it. Compatible with iOS 7+, but dismiss on tap works on 8+.
class SmartAlert
{
static var backroundWindow: UIWindow!
static var alertController: SDCAlertController!
class func showAlertWithTitle(title: String!, message: String!, actionTitle: String)
{
if (iOS8)
{
self.backroundWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
self.backroundWindow.backgroundColor = UIColor.clearColor()
self.backroundWindow.rootViewController = EmptyViewController()
self.backroundWindow.windowLevel = UIWindowLevelAlert
self.backroundWindow.makeKeyAndVisible()
}
self.alertController = SDCAlertController(title: title, message: message, preferredStyle: .Alert)
self.alertController.addAction(SDCAlertAction(title: actionTitle, style: .Default, handler:
{
(_) -> Void in
self.backroundWindow = nil
}))
self.alertController.presentWithCompletion(nil)
}
class func dismissAlert()
{
self.alertController.dismissWithCompletion
{
self.backroundWindow = nil
}
}
}
class EmptyViewController: UIViewController
{
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
SmartAlert.dismissAlert()
}
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent)
{
if motion == UIEventSubtype.MotionShake
{
SmartAlert.dismissAlert()
}
}
}

ChikabuZ
- 10,031
- 5
- 63
- 86