UIAlertController handle dismiss upon click outside (IPad) - ios

Previous to iOS8 we used the UIActionSheet for showing alert and now we need to use the UIAlertController.
When we used the UIActionSheet we could easily handle situations where the user clicked outside the pop up (which means he want to cancel the operation) by comparing the clickedButtonAtIndex to the cancelButtonIndex - if the user indeed pressed outside the popup we got the cancel button index in this function.
How can we handle these situations with the new UIAlertController? I tried to use the "completion" block but it doesn't have any context. Is there an easy way to handle this? (other than "saving" the actions states in some general variable).

You can add an action with style:UIAlertActionStyleCancel and the handler for this action is called when the user taps outside the popup.
if ([UIAlertController class]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %# or tapped elsewhere",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
UIControl *aControl = (UIControl *) sender;
CGRect frameInView = [aControl convertRect:aControl.bounds toView:self.view];
alertController.popoverPresentationController.sourceRect = frameInView;
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:alertController animated:YES completion:nil];
}

The solution which works for UIAlertController with alert style. Just needed to add gesture recognizer to alertController superview.
[self presentViewController: alertController
animated: YES
completion:^{
alertController.view.superview.userInteractionEnabled = YES;
[alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
}];
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}

UITapGestureRecognizer didn't work for me so I've used this way:
func addDismissControl(_ toView: UIView) {
let dismissControl = UIControl()
dismissControl.addTarget(self, action: #selector(self.dismissAlertController), for: .touchDown)
dismissControl.frame = toView.superview?.frame ?? CGRect.zero
toView.superview?.insertSubview(dismissControl, belowSubview: toView)
}
func dismissAlertController() {
self.dismiss(animated: true, completion: nil)
}
func presentAlertController(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil, completion: (() -> Swift.Void)? = nil) {
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default) { (alertAction) -> Void in
handler?(alertAction)
})
self.present(alertController, animated: true, completion: {
self.addDismissControl(alertController.view)
completion?()
})
}
func someWhereInYourViewController() {
// ...
presentAlertController(title: "SomeTitle", message: "SomeMessage", preferredStyle: .actionSheet, handler: { (alertAction) -> Void in
//do some action
}, completion: {
//do something after presentation
})
// ...
}

Related

UIActionSheet deprecated on iOS8 [duplicate]

This question already has an answer here:
UIActionsheet in Ios8
(1 answer)
Closed 6 years ago.
I want to use UIActionSheet for iOS8 but it's deprecated and I don't know how to use the updated way to use this...
See the old code:
-(void)acoesDoController:(UIViewController *)controller{
self.controller = controller;
UIActionSheet *opcoes = [[UIActionSheet alloc]initWithTitle:self.contato.nome delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles:#"other", nil];
[opcoes showInView:controller.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//switch case of the buttons
}
Just to make clear, in this example the action sheet is activated after a long press in an UITableView index.
How can I implement the code above in the properly way?
You can use UIAlertController for the same.
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Action Sheet" message:#"alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
Note : Please Find the answer in Swift as well.
var actionSheet = UIAlertController(title: "Action Sheet", message: "alert controller", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
// Cancel button tappped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { action in
// Distructive button tapped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Other", style: .default, handler: { action in
// OK button tapped.
self.dismiss(animated: true) {
}
}))
// Present action sheet.
present(actionSheet, animated: true)
// Answer for SwiftUI
struct ContentView: View {
#State private var showingOptions = false
#State private var selection = "None"
var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
Button("Red") {
selection = "Red"
}
Button("Green") {
selection = "Green"
}
Button("Blue") {
selection = "Blue"
}
}
}
}
}

IOS create UIAlertViewController programmatically

I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController
I have declare propery in .m
#property (nonatomic, strong) UIAlertController *alertController;
And init in loadview method
//alertviewController
_alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil];
And call the alertview in viewDidLoad:
_alertController = [UIAlertController alertControllerWithTitle:#"Error display content" message:#"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];
// viewController.showNavBarBackButton = YES;
[[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController];
}];
[_alertController addAction:ok];
[self presentViewController:_alertController animated:YES completion:nil];
I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController programmatically?
- (void)logoutButtonPressed
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Logout"
message:#"Are You Sure Want to Logout!"
preferredStyle:UIAlertControllerStyleAlert];
//Add Buttons
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Yes"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
[self clearAllData];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
//Add your buttons to alert controller
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
}
In Xcode 9.4.1
Create alert function globally and use every where.
//Alert function
- (void) showAlertMsg:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message {
UIAlertController * alert = [UIAlertController alertControllerWithTitle : title
message : message
preferredStyle : UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{ }];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[viewController presentViewController:alert animated:YES completion:nil];
});
}
Call like this in your required place.
Import that class and create instance
//Ex:
GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];
//Call alert function with instance
[gcvc showAlertMsg:self title:#"" message:placeholderText];
How would I create a UIAlertView in Swift?
And in
Swift >=3:
let alertController = UIAlertController(title: "Some Error",
message: "Pleas confirm?",
preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self?.present(alertController, animated: true, completion: nil)
Create global Alert controller that is accessible in all view controllers using extension.
Create an extension of UIViewController with your function to display alert with required parameter arguments.
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
Now call this function from your view controller:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}

How to add textField in UIAlertController?

I want to realize a function about changing password. It requires users to input their previous password in an alert dialog.
I want to click the button "Confirm the modification" then jump to the other view controller for changing password. I have written some code, but I don't know how to write in the next moment.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Change password" message:#"Please input your previous password" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"please input your previous password";
textField.secureTextEntry = YES;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleCancel handlers:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Confirm the modification" style:UIAlertActionStyleDestructive handler:*(UIAlertAction *alertAction) {
if (condition) {
statements
}
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
You will get all added textfields from alert controller by its textFields readonly property, you can use it to get its text.
Like
Swift 4:
let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
alertController.addTextField { textField in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
}
let confirmAction = UIAlertAction(title: "OK", style: .default) { [weak alertController] _ in
guard let alertController = alertController, let textField = alertController.textFields?.first else { return }
print("Current password \(String(describing: textField.text))")
//compare the current password and do action here
}
alertController.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
Note: textField.text is optional, unwrap it before using
Objective-C:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"" message:#"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = #"Current password";
textField.secureTextEntry = YES;
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(#"Current password %#", [[alertController textFields][0] text]);
//compare the current password and do action here
}];
[alertController addAction:confirmAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(#"Canelled");
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
By [[alertController textFields][0] text] this line, it will take first textfield added to the alerController and get its text.
You can add multiple textfields to alert controller and access them with the alert controller's textFields property
If the new password is an empty string, present the alert again. Or another way would be to disable the "Confirm" button, enabling it only when text field has text.
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"confirm the modification" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
UITextField *password = alertController.textFields.firstObject;
if (![password.text isEqualToString:#""]) {
//change password
}
else{
[self presentViewController:alertController animated:YES completion:nil];
}
}];
Here is an updated answer for Swift 4.0 that creates the desired kind of textfield:
// Create a standard UIAlertController
let alertController = UIAlertController(title: "Password Entry", message: "", preferredStyle: .alert)
// Add a textField to your controller, with a placeholder value & secure entry enabled
alertController.addTextField { textField in
textField.placeholder = "Enter password"
textField.isSecureTextEntry = true
textField.textAlignment = .center
}
// A cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
print("Canelled")
}
// This action handles your confirmation action
let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in
print("Current password value: \(alertController.textFields?.first?.text ?? "None")")
}
// Add the actions, the order here does not matter
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
// Present to user
present(alertController, animated: true, completion: nil)
And how it looks when first presented:
And while accepting text:
Swift 5.1
#objc func promptAddDialog() {
let ac = UIAlertController.init(title: "Enter answer", message: nil, preferredStyle: .alert)
ac.addTextField{ textField in
textField.placeholder = "Answer"
textField.textAlignment = .center
}
let submitAction = UIAlertAction(title: "Submit", style: .default) {
[weak self, weak ac] _ in
guard let answer = ac?.textFields?[0].text else { return }
self?.submit(answer)
}
ac.addAction(submitAction)
present(ac, animated: true)
}

How to add dynamic buttons on UIAlertController?

I'm converting my code from using UIActionSheet to use UIAlertController.
The way I do it using UIActionSheet is like this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Gender"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
[actionSheet addButtonWithTitle:gender];
}
[actionSheet addButtonWithTitle:#"Cancel"];
And just handle what button is pressed on the delegate method of action sheet.
While converting it to alert controller, I noticed that there is a handler on each of the alert action. I wonder how will I implement the alert controller to have dynamic buttons that I can handle the actions.
Here i mention code for load array into UIAlertController dynamically with image & text :Output Image here
NSArray *numbersArrayList = #[#"One", #"Two", #"Three", #"Four", #"Five", #"Six"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Numbers:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (int j =0 ; j<numbersArrayList.count; j++){
NSString *titleString = numbersArrayList[j];
UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(#"Selected Value: %#",numbersArrayList[j]);
}];
[action setValue:[[UIImage imageNamed:#"USA16.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
Got it!
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Gender:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
UIAlertAction *action = [UIAlertAction actionWithTitle:gender
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSString *title = action.title;
//you can check here on what button is pressed using title
}];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
In Swift:
import Foundation
import UIKit
class AlertUtility {
static let CancelButtonIndex = -1;
class func showAlert(_ onController:UIViewController, title:String?,message:String? ) {
showAlert(onController, title: title, message: message, cancelButton: "OK", buttons: nil, actions: nil)
}
/**
- parameter title: title for the alert
- parameter message: message for alert
- parameter cancelButton: title for cancel button
- parameter buttons: array of string for title for other buttons
- parameter actions: action is the callback which return the action and index of the button which was pressed
*/
class func showAlert(_ onController:UIViewController?, title:String?,message:String? = nil ,cancelButton:String = "OK",buttons:[String]? = nil,actions:((_ alertAction:UIAlertAction,_ index:Int)->())? = nil) {
// make sure it would be run on main queue
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: cancelButton, style: UIAlertAction.Style.cancel) { (action) in
alertController.dismiss(animated: true, completion: nil)
actions?(action,CancelButtonIndex)
}
alertController.addAction(action)
if let _buttons = buttons {
for button in _buttons {
let action = UIAlertAction(title: button, style: .default) { (action) in
let index = _buttons.index(of: action.title!)
actions?(action,index!)
}
alertController.addAction(action)
}
}
if let onController = onController{
onController.present(alertController, animated: true, completion: nil)
}else{
// let appdel = UIApplication.shared.delegate as! AppDelegate
// appdel.window!.rootViewController?.present(alertController, animated: true, completion: nil)
}
}
}
Use in your VC:
//MARK:- IB-ACTION(S)
#IBAction func actionShowAlertButtonTapped(_ sender: Any) {
AlertUtility.showAlert(self, title: "Are you sure you want to start this order ?", cancelButton: "Cancel", buttons: ["OK", "Button 1", "Button 2", "Button 3", "Button 4","Button 5"], actions: {(action, index) -> () in
if index != AlertUtility.CancelButtonIndex {
//Cancel Button Action Here..
}else{
//Other Button Actions as per Button Index..
}
})
}

How to dismiss UIAlertController when tap outside the UIAlertController?

How to dismiss UIAlertController when tap outside the UIAlertController?
I can add a UIAlertAction of style UIAlertActionStyleCancel to dismiss the UIAlertController.
But I want to add the function that when user tap outside the UIAlertController the UIAlertController will dismiss. How to do that? Thank you.
If you are targeting devices having iOS > 9.3 and using Swift and preferredStyle is Alert you can use snippet as below:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:{
alert.view.superview?.userInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
})
}
func alertControllerBackgroundTapped()
{
self.dismissViewControllerAnimated(true, completion: nil)
}
With swift 3:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert)
self.present(alert, animated: true) {
alert.view.superview?.isUserInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
}
}
func alertControllerBackgroundTapped()
{
self.dismiss(animated: true, completion: nil)
}
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
}))
Swift, Xcode 9
Dismiss AlertController with cancel button
provide action to your alertController where UIAlertAction's style is .cancel
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
Using this method alertController will be dismissed when user will tap to cancel action button as well as outside of the alertController.
if you don't want user to dismiss alertController after touch up outside of alertController, disable user interaction of first subviews of alertController in completion closure of present method.
self.present(alertController, animated: true) {
alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
}
Dismiss AlertController on touchup outside of Controller view
If you don't want cancel button in your controller view and want to dismiss controller when user touchup outside of controller view, do so
self.present(alertController, animated: true) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissAlertController))
alertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
}
#objc func dismissAlertController(){
self.dismiss(animated: true, completion: nil)
}
If you are using Swift :
Add an action with addAction(_:) and style:UIAlertActionStyle.Cancel.
The `handler will be called when ou tap on the button or outside the frame.
var alertVC = UIAlertController(...) // initialize your Alert View Controller
alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alertAction: UIAlertAction!) in
alertVC.dismissViewControllerAnimated(true, completion: nil)
}))
Objective-C :
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}]];
Swift 4:
Dismiss Action Sheet when User Taps outside Action Sheet created using UIAlertController
Code Snippet:
// Declare Action Sheet reference
var actionSheet: UIAlertController!
// Init and Show Action Sheet
func showActionSheetClicked(sender: UIButton) {
// Init Action Sheet
actionSheet = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
self.present(actionSheet, animated: true) {
// Enabling Interaction for Transperent Full Screen Overlay
self.actionSheet.view.superview?.subviews.first?.isUserInteractionEnabled = true
// Adding Tap Gesture to Overlay
self.actionSheet.view.superview?.subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.actionSheetBackgroundTapped)))
}
}
// To dismiss Action Sheet on Tap
#objc func actionSheetBackgroundTapped() {
self.actionSheet.dismiss(animated: true, completion: nil)
}
The easiest way in Obj-C:
UIAlertController *alert = [UIAlertController alertControllerWithTitle: ...
[self presentViewController:alert animated:YES completion:^{
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(alertControllerBackgroundTapped)]];
}];
and then:
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}
- (void)addBackgroundDismissTapForAlert:(UIAlertController *)alert {
if (!alert.view.superview) {
return;
}
alert.view.superview.userInteractionEnabled = YES;
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
for (UIView *subV in alert.view.superview.subviews) {
if (subV.width && subV.height) {
subV.userInteractionEnabled = YES;
[subV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
}
}
}
- (void)alertControllerBackgroundTapped {
[self dismissViewControllerAnimated: YES
completion: nil];
}
UIView *alertView = self.alertController.view;
UIView *superPuperView = self.alertController.view.superview;
CGPoint tapCoord = [tap locationInView:superPuperView];
if (!CGRectContainsPoint(alertView.frame, tapCoord)) {
//dismiss alert view
}
If you view debug the superview of the alert, you see its not as simple as adding a tap gesture recognizer to the UITransitionView of the _UIAlertControllerView.You can do this instead
[presenter presentViewController:alertController animated:YES completion:^{
NSArray <UIView *>* superviewSubviews = alertController.view.superview.subviews;
for (UIView *subview in superviewSubviews) {
if (CGRectEqualToRect(subview.bounds, weakSelf.view.bounds)) {
[subview addSingleTapGestureWithTarget:weakSelf action:#selector(dismissModalTestViewController)];
}
}
}];
On iOS 15 it appears the view hierarchy for UIAlertController has changed yet again. It's presented as a new UIWindow that contains the controller itself. So in order to dismiss on tap outside:
present(alertController, animated: true) { [weak self] in
guard let self = self else { return }
let dismissGesture = UITapGestureRecognizer(target: self, action: #selector(self.shouldDismiss))
self.alertController.view.window?.isUserInteractionEnabled = true
self.alertController.view.window?.addGestureRecognizer(dismissGesture)
}
and for shouldDismiss function:
#objc private func shouldDismiss() {
alertController.dismiss(animated: true)
}
The simplest way:
- (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];
}

Resources