iOS How to Dismiss Uialertview with One Tap Anywhere

How to dismiss a UIAlertView when touching anywhere outside of it?

when you present your alert call it with a completion handler like this:

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

Note: Pre Swift 2.2 replace

#selector(self.alertClose(_:))

with

Selector("alertClose")

then create your alert close function:

func alertClose(gesture: UITapGestureRecognizer) {
self.dismissViewControllerAnimated(true, completion: nil)
}

I tested this with swift 2.2 on an iPhone 6

iOS How to dismiss UIAlertView with one tap anywhere?

It sounds like you are essentially trying to recreate a "Toast" on iOS. Good news, someone has already done that. See this project.

Edit: Don't want to use iToast. I like your style, less code it is. Here is what I come up with. It would seem obvious as others have said that the only way to overcome the modal nature of the UIAlertView is to add a superview to handle touch events. But you don't have to do that manually every time, consider subclassing UIAlertView. Try something like this:

Edit: @wagashi, Thanks for accepting my answer, and thanks for the heads up about setFrame: being a good place to adjust the size. Your code does make a very toast-like little alert, however when I tried it I found that if the message was to long the view seemed to fall apart. So I have modified setFrame: to simply reduce the size of the alert by about the size of one button, and to remain centered on the screen. So that the class accurately answers the question title "iOS How to dismiss UIAlertView with one tap anywhere?"

NoButtonAlertView.h

#import <UIKit/UIKit.h>
@interface _NoButtonAlertViewCover : UIView
@property (nonatomic,assign) UIAlertView *delegate;
@end
@interface NoButtonAlertView : UIAlertView
-(id)initWithTitle:(NSString *)title message:(NSString *)message;
@end

NoButtonAlertView.m

#import "NoButtonAlertView.h"
@implementation _NoButtonAlertViewCover
@synthesize delegate = _delegate;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeFromSuperview];
[_delegate dismissWithClickedButtonIndex:0 animated:YES];
}
@end
@implementation NoButtonAlertView
-(void)show{
[super show];
_NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds];
cover.userInteractionEnabled = YES;
cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01];
cover.delegate = self;
[self.superview addSubview:cover];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message{
if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){
}
return self;
}
- (void)setFrame:(CGRect)rect {
// Called multiple times, 4 of those times count, so to reduce height by 40
rect.size.height -= 10;
self.center = self.superview.center;
[super setFrame:rect];
}
@end

With this simple UIAlertView subclass and its UIView subclass for a cover, you can use it as simply as you would a standard UIAlertView. Like so:

NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."];
[alert show];

Will yield:

Custom AlertView

How to dismiss UIAlertController when tap outside the UIAlertController?

Add a separate cancel action with style UIAlertActionStyleCancel. So that when user taps outside, you would get the callback.

Obj-c

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
}]];

Swift 5.0

let alertController = UIAlertController(title: "Alert Title", message: "A Message", preferredStyle: .actionSheet)             
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {
action in
// Called when user taps outside
}))

UIAlertView turn off auto dismiss

You'll need to create your own UI component if you want that behavior. UIAlertView is designed to display some basic information and give you some number of button options that once clicked, close the alert view.

UIAlertView:Dismiss UIAlertView on click of out side the alert box

You need to call - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

When you tap anywhere else

This piece of code will handle your tap

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
[self dismissWithClickedButtonIndex:index animated:YES];
}

How to dismiss SDCAlertView with tap gesture?

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:].



Related Topics



Leave a reply



Submit