Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UIAlertController height?

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?

like image 681
szuniverse Avatar asked Jul 29 '14 15:07

szuniverse


5 Answers

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)
like image 122
Barrett Avatar answered Nov 12 '22 19:11

Barrett


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.

like image 37
julien Avatar answered Nov 12 '22 19:11

julien


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)
like image 21
Amal T S Avatar answered Nov 12 '22 18:11

Amal T S


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.

like image 41
HeadOnn Avatar answered Nov 12 '22 19:11

HeadOnn


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];
like image 1
Ber.to Avatar answered Nov 12 '22 20:11

Ber.to