How to update the constant height constraint of a UIView programmatically?

后端 未结 9 2319
执笔经年
执笔经年 2020-11-27 11:16

I have a UIView and I set the constraints using Xcode Interface Builder.

Now I need to update that UIView instance\'s height constant progra

9条回答
  •  佛祖请我去吃肉
    2020-11-27 12:03

    Drag the constraint into your VC as an IBOutlet. Then you can change its associated value (and other properties; check the documentation):

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

提交回复
热议问题