Swift 如何更改控制器高度?
我创建了一个UIAlertControllerSwift 如何更改控制器高度?,swift,height,uialertcontroller,Swift,Height,Uialertcontroller,我创建了一个UIAlertController let alertC = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alertC.addTextFieldWithConfigurationHandler(addTextField) alertC.addAction(UIAlertAction(title: "Cancel", style:
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)
但在那之后,我想改变控制器的高度?如何执行此操作?我发现您可以在显示视图控制器之前添加约束
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)
由于没有要输入的消息,我在消息字段中添加了带“\n\n\n”的行,以使警报控制器的高度更长 如果这对任何人都有帮助,接受的答案将改变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()
}
*注意:不要忘记停用默认宽度约束,以避免冲突约束。
默认高度约束不会与添加的高度约束发生冲突。但您也可以删除一致代码的默认高度约束。Swift 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)
我知道这个问题是用Swift提出的,但它对我很有用,我用的是objective-c,所以 在目标-C中:
NSLayoutConstraint *heigth = [NSLayoutConstraint constraintWithItem:alertController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:400];
[alertController.view addConstraint:heigth];
@JoJoe,其中一个是
UIAlertView
,这是新的iOS 8UIAlertController
@JoJoe请删除重复的标志。。。因为它没有复制你不知道我在问什么……你为什么要改变它的高度?你想要什么样的外观?我想在这个视图中添加一些文本字段,在这些文本字段下面我想添加一个uitable视图。。因此,我需要增加高度,因为TableView很棒,你节省了我的时间:)你应该能够将swift代码转移到objective cAnd,我这样做了,这就是我建议的代码,以帮助任何有相同问题并想使用@barrett解决方案但在objective-c中的人节省时间。