20

我创建了一个 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 的高度?我怎样才能做到这一点?

4

5 回答 5

56

我发现您可以在呈现视图控制器之前添加约束

 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)
于 2015-02-10T06:17:19.920 回答
7

由于我没有要输入的消息,我在消息字段中添加了带有“\n \n \n”的行,以使警报控制器的高度更长。

于 2016-01-26T17:40:06.720 回答
3

如果这对任何人都有帮助,那么接受的答案将改变 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()
}

*注意:不要忘记禁用默认宽度约束,以避免冲突约束。默认高度约束不会与添加的高度约束发生冲突。但是对于连贯代码,您也可以删除默认高度约束。

于 2017-07-17T17:53:22.477 回答
3

斯威夫特 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)
于 2019-04-10T18:21:18.227 回答
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];
于 2020-10-23T10:47:00.470 回答