简体   繁体   English

如何在Swift中更改UIAlertController中UITextField的高度?

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

I have added a UITextField in a UIAlertController . 我在UIAlertController添加了一个UITextField 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. 它不起作用。

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)
}

添加约束的结果

@ the4kman给出了正确的答案,但是你可以通过使用高度锚来更容易地添加约束:

textField.addConstraint(textField.heightAnchor.constraint(equalToConstant: 100))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以快速更改UIAlertController Buttons的高度和宽度吗? - Can we change the height and width of UIAlertController Buttons in swift 在UISearchBar中更改UITextField的高度-Swift 3 - Change UITextField height in UISearchBar - Swift 3 如何使用Swift更改按钮的高度? - How can I change the height of a button with Swift? 如何在UIAlertController中的UITextField中添加边距? - How do I add margins to UITextField in a UIAlertController? 如何使我在UIAlertController内的UITextField中输入的数据通过Swift中的重置保持不变? - How to make data I enter in a UITextField inside a UIAlertController persist through resets in Swift? Swift如何获取UIAlertController标题高度 - Swift how to get UIAlertController title height 如何在“textFieldDidBeginEditing”中计算和使用键盘高度(快速),以便我可以精确地向上移动 UITextfield? - How to calculate and use keyboard height (in swift) in “textFieldDidBeginEditing” so that I can move UITextfield up precisely? 如何在UIAlertController中为swift覆盖便利init? - How can I override convenience init in UIAlertController for swift? Swift 4 UIAlertController如何将不同的动作组合到1个完成处理程序中? - Swift 4 UIAlertController how can I combine different actions into 1 completion handler? 如何在 Swift 的子视图中访问 UITextfield? - How can I reach UITextfield in subviews in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM