2017-09-12 39 views
1

我在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) 

它不工作。

+0

試試這個【答案】(https://stackoverflow.com/a/37470191/4056108) – chirag90

+1

已經試過,沒有工作:( –

回答

8

這適用於約束

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

+0

謝謝。它的工作:) –

+0

這個作品! +1 –

+0

可以使這個文本框多行嗎? @ the4kman –

1

@ the4kman給出了正確的答案,但你可以通過使用高度錨添加更容易限制:

textField.addConstraint(textField.heightAnchor.constraint(equalToConstant: 100)) 
相關問題