
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to dismiss the Alert with click on outside of the alert in iOS?
Understanding and implementing UIAlert can be tricky especially if you’re new to iOS Development, In this post, we will be seeing how we can dismiss the alert when the user taps outside the alert box.
For this demo, we will be using UIAlert class, to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present(_:animated: completion:) method. UIKit displays alerts and action sheets modally over your app's content.
You can read more about it: https://developer.apple.com/documentation/uikit/uialertcontroller
So let’s get started,
Step 1 − Open Xcode and create a single view application and name it UIAlertSample.
Step 2 − In Main. storyboard add one button, and create @IBAction and name it showAlert,
@IBAction func showAlert(_ sender: Any) { }
So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.
Step 3 − Inside button action showAlert, First create UIAlert object as below
let uialert = UIAlertController(title: "WELCOME", message: "Welcome to my tutorials, tap outside to dismiss the alert", preferredStyle: .alert)
Step 4 − Present the alert and on its completion add a selector as shown below,
self.present(uialert, animated: true, completion:{ uialert.view.superview?.isUserInteractionEnabled = true uialert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.dismissOnTapOutside))) })
Step 5 − Add selector function,
@objc func dismissOnTapOutside(){ self.dismiss(animated: true, completion: nil) }
Step 6 − Run the application,
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } @IBAction func showAlert(_ sender: Any) { let uialert = UIAlertController(title: "WELCOME", message: "Welcome to my tutorials, tap outside to dismiss the alert", preferredStyle: .alert) self.present(uialert, animated: true, completion:{ uialert.view.superview?.isUserInteractionEnabled = true uialert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.dismissOnTapOutside))) }) } @objc func dismissOnTapOutside(){ self.dismiss(animated: true, completion: nil) } }
- Related Articles
- How to dismiss the dialog with click on outside of the dialog?
- How to dismiss the dialog with the click on outside of the dialog box in android?
- How to show Alert Dialog in iOS?
- How to control the width and height of the default Alert Dialog in iOS?
- Trigger a button click and generate alert on form submission in JavaScript
- How to click the 'Ok' button inside an alert window with a Selenium command?
- JavaScript how to get an alert to appear when I click on a button in a class?
- Get input value from TextField in iOS alert in Swift
- How to style "alert" buttons with CSS?
- How to create alert messages with CSS?
- How I can replace a JavaScript alert pop up with a fancy alert box?
- How to change the color of the alert box in JavaScript?
- How to center the JavaScript alert message box?
- How to add list in alert dialog?
- How to show hyperlinks in JavaScript alert?
