如何在Swift中更改UIAlertController中UITextField的高度?
问题描述:
我在UIAlertController
中添加了UITextField
。我想改变文本字段的高度。我试过这种方式:如何在Swift中更改UIAlertController中UITextField的高度?
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)
它不工作。
答
这适用于约束。
alertController.addTextField { textField in
let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
textField.addConstraint(heightConstraint)
}
+0
谢谢。它的工作:) –
+0
这个作品! +1 –
+0
可以使这个文本框多行吗? @ the4kman –
答
@ the4kman给出了正确的答案,但你可以通过使用高度锚添加更容易限制:
textField.addConstraint(textField.heightAnchor.constraint(equalToConstant: 100))
试试这个【答案】(https://stackoverflow.com/a/37470191/4056108) – chirag90
已经试过,没有工作:( –