我创建了一个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)
但之后我想改变UIAlertController的高度?我怎么能这样做?
答案 0 :(得分:43)
我发现您可以在呈现视图控制器之前添加约束
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: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80)
alertController.view.addConstraint(height);
self.present(alertController, animated: true, completion: nil)
答案 1 :(得分:7)
由于我没有输入消息,因此添加了" \ n \ n \ n"在消息字段中,使警报控制器的高度更长。
答案 2 :(得分:2)
如果这对任何人有帮助,接受的答案将改变UIAlertController的高度,但不会改变宽度。因此,更改UIAlertController的高度和宽度的更好方法是更改UIAlertController的其中一个子视图的约束,而不是直接更改其视图。
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()
}
*注意:不要忘记停用默认宽度约束,以避免冲突约束。 默认高度约束不会导致与添加的高度约束冲突。但是你可以删除默认的高度约束以及相干代码。
答案 3 :(得分:1)
我知道问题出在Swift中,但这对我来说真的很有用,我正在使用Objective-C,所以...
在 Objective-C 中:
NSLayoutConstraint *heigth = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:400];
[alertController.view addConstraint:heigth];
答案 4 :(得分:0)
快速5
let alert = UIAlertController(title: "Title", message: "New Message", preferredStyle: UIAlertController.Style.alert)
let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 350)
alert.view.addConstraint(height)
let okAction = UIAlertAction(title: "Done", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
// Perform Action
})
alert.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)