How to change UIAlertController height?
Asked Answered
C

5

22

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?

Coronet answered 29/7, 2014 at 15:56 Comment(4)
@JoJoe That one is UIAlertView, this is the new iOS 8 UIAlertController.Cambyses
@JoJoe pls remove the duplicated flag... because its not duplicated you dont know what I asked......Coronet
Why do you want to change its height? What appearance are you striving for?Sidewalk
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 tableviewCoronet
L
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)
Laaspere answered 10/2, 2015 at 6:17 Comment(0)
I
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.

Interactive answered 26/1, 2016 at 17:40 Comment(1)
This could be used as a crunch. Overall it's bad practice to adjust height with empty lines because it's mixing up responsibility.Hyperbaton
E
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)
Electrometallurgy answered 10/4, 2019 at 18:21 Comment(0)
C
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.

Catercorner answered 17/7, 2017 at 17:53 Comment(0)
E
2

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];
Edithe answered 23/10, 2020 at 10:47 Comment(2)
you should be able to transfer the swift code to objective-cCoronet
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.Edithe

© 2022 - 2024 — McMap. All rights reserved.