3

As everyone's aware, UIAlertView is deprecated now and Apple wants us to use the new UIAlertController with the style UIAlertControllerStyleAlert. However, using the UIAlertControllerStyleAlert you cannot trigger the UIAlertAction with style UIAlertActionStyleCancel by tapping outside the alert view.

Does anyone know of a way to dismiss the alert view by tapping outside of it?

Cheers

rmaddy
  • 314,917
  • 42
  • 532
  • 579
micnguyen
  • 1,419
  • 18
  • 25
  • `UIAlertView` didn't support that either. You always need to tap one of the buttons. – rmaddy Apr 24 '15 at 04:08
  • Oh really? I wonder why then that sort of behaviour is so ingrained into alerts for me. Would you have a suggestion as to how I could achieve that? – micnguyen Apr 24 '15 at 04:15
  • 1
    On the iPad, `UIActionSheet` could be dismissed by tapping outside, but that has never been the case (on any iOS devices) for `UIAlertView`. – rmaddy Apr 24 '15 at 04:17
  • I see. It seems that is also the case on iPhones with UIActionSheet. I've just tried some hacky things to achieve tap-to-dismiss on an Alert style such as adding a tap gesture on the presenting view controller, but to no avail. – micnguyen Apr 24 '15 at 04:20
  • May be you are looking for http://stackoverflow.com/questions/8260562/ios-how-to-dismiss-uialertview-with-one-tap-anywhere – Tapas Pal Apr 24 '15 at 05:20

2 Answers2

2

You can add a separate cancel action with style UIAlertActionStyleCancel for the alertViewController so that when user taps outside, you would get the callback.

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
 }]];
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Kaey
  • 4,615
  • 1
  • 14
  • 18
1

Try this code:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self button];

}

- (void) button {
    UIButton * AlertButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [AlertButton setTitle:@"Button" forState:UIControlStateNormal];
    AlertButton.frame = CGRectMake((self.view.frame.size.width/2) - 50 , (self.view.frame.size.height/2) - 25, 100, 50);
    [AlertButton addTarget:self action:@selector(Alert) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:AlertButton];
}

- (void)Alert {
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController: alert animated: YES completion:^{ alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(DismissAlertByTab)]]; }];
}

- (void)DismissAlertByTab
{
    [self dismissViewControllerAnimated: YES completion: nil];
}
Ibrahim
  • 6,006
  • 3
  • 39
  • 50