Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the height of UITextField in UIAlertController in Swift?

I have added a UITextField in a UIAlertController. I want to change the height of the text field. I have tried this way:

let alertController = UIAlertController(title: "Comments", message: "Write your comments here", preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField { (textField : UITextField) -> Void in
    var frame: CGRect = textField.frame
    frame.size.height = 100
    textField.frame = frame
    textField.placeholder = "comment"
    print(textField.frame.size.height)
}

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (result : UIAlertAction) -> Void in
}

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
}

alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

It's not working.

like image 471
Ilias Ahmed Avatar asked Sep 12 '17 08:09

Ilias Ahmed


1 Answers

This works with a constraint.

alertController.addTextField { textField in
    let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
    textField.addConstraint(heightConstraint)
}

result of adding the constraint

like image 155
Tamás Sengel Avatar answered Sep 21 '22 14:09

Tamás Sengel