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

后端 未结 9 2297
执笔经年
执笔经年 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:00

    If you have a view with multiple constrains, a much easier way without having to create multiple outlets would be:

    In interface builder, give each constraint you wish to modify an identifier:

    Then in code you can modify multiple constraints like so:

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

    You can give multiple constrains the same identifier thus allowing you to group together constrains and modify all at once.

提交回复
热议问题