
- 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 create a Custom Dialog box on iOS App using Swift?
To create a dialog box in swift we’ll make use of UIAlertController which is an important part of UIKit. We’ll do this with help of an iOS application and a sample project.
First of all, we’ll create an empty project, then inside its default view controller, we’ll perform the following operations.
We’ll create an UIAlertController object.
let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert)
We’ll create an action
let okAction = UIAlertAction.init(title: "Ok", style: .default) { _ in print("You tapped ok") //custom action here. }
We’ll add the action to the alert and present it
alert.addAction(okAction) self.present(alert, animated: true, completion: nil)
Now we’ll convert this to a function −
func createAlert(withTitle title:String,andDescription description: String) { let alert = UIAlertController.init(title: title, message: description, preferredStyle: .alert) let okAction = UIAlertAction.init(title: "Ok", style: .default) { _ in print("You tapped ok") //custom action here. } alert.addAction(okAction) self.present(alert, animated: true, completion: nil) }
we’ll now call the function in our viewWillLayoutSubviews method, and this is how it looks when we run this on a device.
override func viewWillLayoutSubviews() { self.createAlert(withTitle: "This is an alert", andDescription: "Enter your description here.") }
This produces the result as shown below.
- Related Articles
- How to create a Custom Dialog box on Android?
- How to create a WebView in an iOS App using Swift?
- How to make an HTTP request on iOS App using Swift?
- How to make an HTTP POST request on iOS App using Swift?
- How to load and display an image on iOS App using Swift?
- How to display an image with rounded corners on iOS App using Swift?
- How to create scrollable TextView on iOS App?
- How to hide the status bar in a iOS App using Swift?
- How to rotate an image in imageview by an angle on iOS App using Swift?
- How to create multiple styles inside a TextView on iOS App?
- How to create Dialog Box in ReactJS?
- How to create a Dialog Box without a title in Android using Kotlin?
- How to integrate a facebook login in swift for iOS App?
- How to create a Confirmation Dialog Box in Java?
- How to create a Dialog Box without a title in Android?

Advertisements