21

I created an UIAlertController

let alertC = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alertC.addTextFieldWithConfigurationHandler(addTextField)
alertC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
alertC.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: okButton))
presentViewController(alertC, animated: true, completion: nil)

But after that I would like to change the UIAlertController height? How can I do this?

szuniverse
  • 1,076
  • 4
  • 17
  • 32
  • @JoJoe That one is `UIAlertView`, this is the new iOS 8 `UIAlertController`. – Pascal Jul 29 '14 at 19:34
  • @JoJoe pls remove the duplicated flag... because its not duplicated you dont know what I asked...... – szuniverse Jul 29 '14 at 20:39
  • Why do you want to change its height? What appearance are you striving for? – Peter Hajas Aug 09 '14 at 18:29
  • I would like to do add some textfield to this view and below these textfields I would like to add a uitable view.. So I needed to increase the height due to the tableview – szuniverse Aug 10 '14 at 22:19

5 Answers5

58

I found you can add constraints before you present the view controller

 let alertController = UIAlertController(title: nil, message: "hello", preferredStyle: .alert)
    
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
        // hide action sheet
    }
    alertController.addAction(cancelAction)
    
   
    var height:NSLayoutConstraint = NSLayoutConstraint(
        item: alertController.view, attribute: NSLayoutConstraint.Attribute.height,
        relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, 
        attribute: NSLayoutConstraint.Attribute.notAnAttribute, 
        multiplier: 1, constant: self.view.frame.height * 0.80)
    alertController.view.addConstraint(height);
    self.present(alertController, animated: true, completion: nil)
Robert
  • 39,162
  • 17
  • 99
  • 152
Barrett
  • 1,030
  • 1
  • 15
  • 28
7

Since I did not have a message to input I added lines with "\n \n \n" in the message field to make the alert controller height longer.

julien
  • 437
  • 4
  • 9
  • This could be used as a crunch. Overall it's bad practice to adjust height with empty lines because it's mixing up responsibility. – IliaEremin Jul 21 '21 at 12:40
4

Swift 5

        let alert = UIAlertController(title: "Title", message: "New Message", preferredStyle: UIAlertController.Style.alert)

        let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 350)
        alert.view.addConstraint(height)


        let okAction = UIAlertAction(title: "Done", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            // Perform Action
        })
        alert.addAction(okAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
Amal T S
  • 3,327
  • 2
  • 24
  • 57
3

If this helps anyone, the accepted answer will change the height of UIAlertController, but not the width. So the better way to change both height and width of UIAlertController is change constraints on one of the subviews of UIAlertController rather than its view directly.

override func updateViewConstraints()
{
let widthConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.view.subviews[0], attribute:
  NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 120.0)

let heightConstraint:NSLayoutConstraint = NSLayoutConstraint(item: self.view.subviews[0], attribute:
  NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute:
  NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 120.0)

for constraint in self.view.subviews[0].constraints {
  if constraint.firstAttribute == NSLayoutAttribute.width && constraint.constant == 270{
    NSLayoutConstraint.deactivate([constraint])
    break
  }
}

self.view.subviews[0].addConstraint(widthConstraint)
self.view.subviews[0].addConstraint(heightConstraint)

super.updateViewConstraints()
}

*NOTE: Do not forget to deactivate default width constraint, to avoid conflicting constraints. Default height constraint won't cause conflict with added height constraint. But you can remove default height constraint as well for coherent code.

HeadOnn
  • 1,480
  • 14
  • 21
1

I know that the question was in Swift, but it was really usefull for me and i'm useing objective-c so...

In Objective-C:

NSLayoutConstraint *heigth = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:400];

[alertController.view addConstraint:heigth];
Ber.to
  • 1,064
  • 1
  • 10
  • 15
  • you should be able to transfer the swift code to objective-c – szuniverse Oct 28 '20 at 12:35
  • 1
    And i did, and that's the code i propose in order to help to save time to anyone who has the same question and wants to use the @barrett solution but in objective-c. – Ber.to Oct 29 '20 at 13:12