How to change UIAlertController height?

szuniverse picture szuniverse · Jul 29, 2014 · Viewed 23.7k times · Source

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?

Answer

Barrett picture Barrett · Feb 10, 2015

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)