

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 load and display an image on iOS App using Swift?
- How to make an HTTP POST request 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 integrate a facebook login in swift for iOS App?
- How to create multiple styles inside a TextView on iOS App?
- How to create a Confirmation Dialog Box in Java?
- How to create a Dialog Box without a title in Android using Kotlin?
- How to create a Dialog Box without a title in Android?
- How to show prompt dialog box using JavaScript?
Advertisements