如何改变UIAlertController的高度?

问题描述 投票:13回答:4

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

swift height uialertcontroller
4个回答
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)

7
投票

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


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

*注意:不要忘记停用默认宽度约束,以避免冲突约束。默认高度约束不会导致与添加的高度约束冲突。但是您可以删除默认高度约束以及相干代码。


1
投票

斯威夫特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)
© www.soinside.com 2019 - 2024. All rights reserved.