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)
}