How can I change constraints priority in run time

后端 未结 8 1234
一整个雨季
一整个雨季 2020-12-12 16:07

I have a view which has dynamic height and I am trying to change this view height priority in run time.

Here is my part of code;

if (index == 0) {

          


        
8条回答
  •  死守一世寂寞
    2020-12-12 16:21

    I want to make a small addition to Cyrille's answer.

    If you are creating constraint in code, make sure to set its priority before making it active. For instance:

    surveyViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self
                                                   attribute:NSLayoutAttributeHeight
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.superview
                                                   attribute:NSLayoutAttributeHeight
                                                  multiplier:1
                                                    constant:0];
    surveyViewHeightConstraint.active = YES;
    surveyViewHeightConstraint.priority = 999;
    

    This would result in run-time exception.

    Mutating a priority from required to not on an installed constraint (or vice-versa) is not supported.

    Correct order is:

    surveyViewHeightConstraint.priority = 999;
    surveyViewHeightConstraint.active = YES;
    

    For Swift version 4+

    constraintName.priority = UILayoutPriority(rawValue: 999)
    

提交回复
热议问题