
- 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 control the width and height of the default Alert Dialog in iOS?
There will be instance where you might get a requirement to control / manipulate width and height the Alert while developing iOS application. If you’re not familiar with the same, it can trouble you.
Here we will be seeing how to control the width and height of default alert box, For controlling the height and width we will be using NSLayoutConstraint.
To read more about UIAlertController refer −
https://developer.apple.com/documentation/uikit/uialertcontroller
In this, we will be creating a new project where we will have a button, on tapping that button we will show alert with custom message.
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “changeheightandwidth”
Step 2 − In Main.storyboard create one button and name it tap, create @IBAction in ViewController.swift and name outlet btnAtap.
Step 3 − Write the following code in your button method.
Create object of UIAlertController.
let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert)
Create height and width constraints.
// height constraint let constraintHeight = NSLayoutConstraint( item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100) alert.view.addConstraint(constraintHeight) // width constraint let constraintWidth = NSLayoutConstraint( item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300) alert.view.addConstraint(constraintWidth)
Present the alert view with actions.
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancel) let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil) alert.addAction(OKAY) self.present(alert, animated: true, completion: nil)
Step 4− Run the code.
For complete code,
@IBAction func btnATap(_ sender: Any) { let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert) // height constraint let constraintHeight = NSLayoutConstraint( item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100) alert.view.addConstraint(constraintHeight) // width constraint let constraintWidth = NSLayoutConstraint( item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300) alert.view.addConstraint(constraintWidth) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancel) let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil) alert.addAction(OKAY) self.present(alert, animated: true, completion: nil) }
- Related Articles
- How to control the height and width of the default alert dialog in android?
- How to show Alert Dialog in iOS?
- How I can set the width and height of a JavaScript alert box?
- How to dismiss the Alert with click on outside of the alert in iOS?
- How to add list in alert dialog?
- How to show alert dialog in Android?
- How to add custom view in alert dialog?
- How to use Singleton Alert Dialog in android?
- How to get the height and width of the android.widget.ImageView?
- How to add check box list in alert dialog?
- How to add radio button list in alert dialog?
- How to get the Width and Height of the screen in JavaScript?
- How to display a listView in an android alert dialog?
- How to get the width and height of an android.widget.ImageView in Kotlin?
- How to get the Navigation Bar height in iOS?
