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
}))
Don't dismiss uialertcontroller sheet with tap gesture outside dialog area in iOS
for UIAlertController Type as alert
you can download the sample project
add the gesture recognizer to alertController superview for handle the userinteraction
self.present(alertController, animated: true, completion: {() -> Void in
alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil)
})
on that action do nothing
update
let alertController = UIAlertController(title: "Do something", message: "With this", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Done", style: .default) { action in
// perhaps use action.title here
})
self.present(alertController, animated: true, completion: {() -> Void in
alertController.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
})
for UIAlertController Type as actionSheet
you can download the sample project
you can do this two ways
option 1
alert.view.superview.subviews[0] isUserInteractionEnabled = false
option 2
alert.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
for e.g
self.present(sheet, animated: true, completion: {() -> Void in
// sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
})
full code
let sheet = UIAlertController(title: "karthik", message: "check with", preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: "Create Ticket", style: .default) { (action: UIAlertAction) in
})
sheet.addAction(UIAlertAction(title: "Start VM", style: .default) { (action: UIAlertAction) in
})
sheet.addAction(UIAlertAction(title: "Restart VM", style: .default) { (action: UIAlertAction) in
})
sheet.addAction(UIAlertAction(title: "Stop VM", style: .destructive) { (action: UIAlertAction) in
})
sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action: UIAlertAction) in
})
self.present(sheet, animated: true, completion: {() -> Void in
// sheet.view.superview?.subviews[0].isUserInteractionEnabled = false;
sheet.view.superview?.subviews[0].addGestureRecognizer(UITapGestureRecognizer(target: self, action: nil))
})
Dismiss UIAlertController after few seconds?
May below code will do the work:
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"Please wait, checking your PAID INVOICE, to allow the service."
message:"...waiting..."
preferredStyle:UIAlertControllerStyleAlert];
[self.window.rootViewController presentViewController:alert animated:YES
completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:^{
//Dismissed
}];
});
Related Topics
Push Notification Not Receiving in Background iOS
How to Load an Uiimage into a Swiftui Image Asynchronously
How to Make a Button Continually Call a Function When Held Down (Spritekit)
Optional Binding with Try? and As? Still Produces an Optional Type
Open Phone Settings Programmatically in iOS9
Performance Testing in Swift Using Tdd
Uibutton Image for Normal State in Collectionview Cell Repeats Itself Every Four Cells
How to Manually Deprecate Members
How to Use Namespaces in Swift
Passing Data to View Controllers That Are Embedded in Container Views
iOS Nsdate() Returns Incorrect Time
Filemanager Cannot Find Audio File
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
Simple and Clean Way to Convert JSON String to Object in Swift
Difference Between Dispatch_Async and Dispatch_Sync on Serial Queue
How to Use Avfoundation to Crop a Video
Reading the Gps Data from the Image Returned by the Camera in iOS Iphone