【问题标题】:How to update the constant height constraint of a UIView programmatically?如何以编程方式更新 UIView 的恒定高度约束?
【发布时间】:2017-07-28 21:58:53
【问题描述】:

我有一个UIView,我使用 Xcode Interface Builder 设置了约束。

现在我需要以编程方式更新 UIView 实例的高度常数。

有一个函数类似于myUIView.updateConstraints(),但我不知道如何使用它。

【问题讨论】:

标签: swift xcode autolayout nslayoutconstraint


【解决方案1】:

要更新布局约束,您只需要更新常量属性并在之后调用 layoutIfNeeded。

myConstraint.constant = newValue
myView.layoutIfNeeded()

【讨论】:

    【解决方案2】:

    将约束作为 IBOutlet 拖到您的 VC 中。然后您可以更改其关联值(和其他属性;查看文档):

    @IBOutlet myConstraint : NSLayoutConstraint!
    @IBOutlet myView : UIView!
    
    func updateConstraints() {
        // You should handle UI updates on the main queue, whenever possible
        DispatchQueue.main.async {
            self.myConstraint.constant = 10
            self.myView.layoutIfNeeded()
        }
    }
    

    【讨论】:

    • [weak self] 防止循环保留
    【解决方案3】:

    从界面生成器中选择高度约束并从中取出。所以,当你想改变视图的高度时,你可以使用下面的代码。

    yourHeightConstraintOutlet.constant = someValue
    yourView.layoutIfNeeded()
    

    方法updateConstraints()UIView的实例方法。当您以编程方式设置约束时,它会很有帮助。它更新视图的约束。更多详情请点击here

    【讨论】:

    • 今天我了解到约束可以变成一个出口,从那里我可以做任何我想做的事情。谢谢
    • @ParthAdroja,兄弟你能看看我的问题stackoverflow.com/questions/46034278/… 吗?
    • 我没有 xib 文件,如何以编程方式在 UIView 中设置 yourHeightConstraint?
    • @newszer 你需要查看其他主题stackoverflow.com/questions/31651022/…
    • 我听说我们应该在更改常量约束后调用 layoutIfNeeded。为什么?有时我在 viewWillLayoutSubviews 和 viewDidLayoutSubviews 中调用 layoutIfNeeded。没事吧?
    【解决方案4】:

    如果您有一个具有多个约束的视图,那么无需创建多个出口的更简单的方法是:

    在界面生成器中,给每个要修改的约束一个标识符:

    然后在代码中你可以像这样修改多个约束:

    for constraint in self.view.constraints {
        if constraint.identifier == "myConstraint" {
           constraint.constant = 50
        }
    }
    myView.layoutIfNeeded()
    

    您可以为多个约束赋予相同的标识符,从而允许您将约束组合在一起并一次修改所有约束。

    【讨论】:

    • 这很好用,但我发现只使用常规插座集合来对约束进行分组更容易。例如,根据我正在使用的视图,有 47 个约束,但只有 6 个我想在运行时更改,所以它是一个小得多的循环。这也不需要在两个不同的地方保持字符串同步。
    • 嗨,我用同样的方式,但它永远不会出现在 if 情况下,我给出了相同的标识符。
    • 我想知道是否可以命名约束。很好的答案!
    • 如果要约束的项目在子视图内,请使用外部 for 循环:for subview in view.subviews
    【解决方案5】:

    如果上述方法不起作用,请确保在 Dispatch.main.async{} 块中更新它。你不需要调用 layoutIfNeeded() 方法。

    【讨论】:

      【解决方案6】:

      首先将高度约束连接到我们的视图控制器以创建 IBOutlet,如下面的代码所示

      @IBOutlet weak var select_dateHeight: NSLayoutConstraint!
      

      然后将下面的代码放在视图中确实加载或任何操作中

      self.select_dateHeight.constant = 0 // we can change the height value
      

      如果它在按钮内点击

      @IBAction func Feedback_button(_ sender: Any) {
       self.select_dateHeight.constant = 0
      
      }
      

      【讨论】:

        【解决方案7】:

        在不创建 IBOutlet 的情况下更改 HeightConstraintWidthConstraint

        注意:在 Storyboard 或 XIB 文件中指定高度或宽度限制。使用此扩展获取此约束后。

        您可以使用此扩展来获取高度和宽度约束:

        extension UIView {
        
        var heightConstraint: NSLayoutConstraint? {
            get {
                return constraints.first(where: {
                    $0.firstAttribute == .height && $0.relation == .equal
                })
            }
            set { setNeedsLayout() }
        }
        
        var widthConstraint: NSLayoutConstraint? {
            get {
                return constraints.first(where: {
                    $0.firstAttribute == .width && $0.relation == .equal
                })
            }
            set { setNeedsLayout() }
        }
        
        }
        

        你可以使用:

        yourView.heightConstraint?.constant = newValue 
        

        【讨论】:

        • 有一种方法first(where: ...) 可以立即使用,而不是filter + first
        • @Vyachaslav Gerchicov 最好先使用,因为它自动是filter + first
        【解决方案8】:
        Create an IBOutlet of NSLayoutConstraint of yourView and update the constant value accordingly the condition specifies.
        
        //Connect them from Interface 
        @IBOutlet viewHeight: NSLayoutConstraint! 
        @IBOutlet view: UIView!
        
        private func updateViewHeight(height:Int){
           guard let aView = view, aViewHeight = viewHeight else{
              return
           }
           aViewHeight.constant = height
           aView.layoutIfNeeded()
        }
        

        【讨论】:

          【解决方案9】:

          如果需要,您可以使用平滑动画更新约束,请参见下面的代码块:

          heightOrWidthConstraint.constant = 100
          UIView.animate(withDuration: animateTime, animations:{
          self.view.layoutIfNeeded()
          })
          

          【讨论】:

          • 我想知道为什么即使没有调用动画或 layoutIfNeeded,我的不断变化也会立即显示? iOS 15
          猜你喜欢
          • 2021-12-22
          • 2015-04-02
          • 2015-07-15
          • 2014-01-22
          • 2015-06-10
          • 2018-11-25
          • 2018-03-17
          • 1970-01-01
          • 2023-04-02
          相关资源
          最近更新 更多