【问题标题】:How to change UIAlertController height?如何更改 UIAlertController 高度?
【发布时间】:2014-09-21 02:01:23
【问题描述】:

我创建了一个 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 的高度?我该怎么做?

【问题讨论】:

  • @JoJoe 那是UIAlertView,这是新的iOS 8 UIAlertController
  • @JoJoe 请删除重复的标志......因为它没有重复你不知道我问了什么......
  • 为什么要改变它的高度?你追求什么样的外表?
  • 我想在这个视图中添加一些文本字段,在这些文本字段下面我想添加一个合适的视图..所以我需要增加由于 tableview 的高度

标签: swift height uialertcontroller


【解决方案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];

【讨论】:

  • 你应该可以将 swift 代码转移到 Objective-c
  • 我做到了,这就是我提出的代码,以帮助有相同问题并希望使用 @barrett 解决方案但在 Objective-c 中的任何人节省时间。
【解决方案2】:

我发现你可以在展示视图控制器之前添加约束

 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)

【讨论】:

  • 太棒了,你拯救了我的一天 :))
【解决方案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)

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 这可以用作紧缩。总的来说,用空行调整高度是不好的做法,因为它混淆了责任。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2021-09-01
      • 2020-05-22
      相关资源
      最近更新 更多