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?
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: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80)
alertController.view.addConstraint(height);
self.presentViewController(alertController, animated: true, completion: nil)
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.
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.