When using navigation, you would use the mas_updateConstraints method to update the constraint, but this method only updates the value, not the priority of the constraint.

🌰 such as:

@implementation xxx
- (void)setupConstraints {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        ......
        make.right.equalTo(self.view).offset(- 50).priorityMedium();
        // Left distance 50, highest priority
        make.left.equalTo(self.view).offset(50).priorityHigh(); 
    }];
}

- (void)changePriority:(BOOL)isHigh {
    // Update priority constrained by left distance
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
       if (isHigh) {
           make.left.equalTo(self.view).offset(50).priorityHigh();
       } else {
           make.left.equalTo(self.view).offset(50).priorityLow(); }}];// Refresh the layout
    [self layoutIfNeeded];
}
@end
Copy the code
  • Tests show that this does not update the priority of the constraints and only layouts according to the initial constraints.

I’ll retrieve it for you.

  • Just update the constant: Updates only the value of constant.

However, there is a case where the priority of the same constraint can be changed:

.offset(xxx)

How do I dynamically update the priority of a constraint

Method 1: Strong reference constraints

  • Changes made immediately after initialization (within the same Runloop) do not change, and are applicable to updates made later after initialization.
@interface xxx(a)
@property (nonatomic.strong) MASConstraint *constraint;
@end

@implementation xxx

- (void)setupConstraints {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        ......
        self.constraint = make.right.equalTo(self).priorityLow();
    }];
}

- (void)changePriority:(BOOL)isHigh {
    // Invalidate constraints (internal call uninstall to remove constraints from the group)
    [self.constraint deactivate]; 

    // Reset the priority
    if (isHigh) {
        self.constraint.priorityHigh();
    } else {
        self.constraint.priorityLow();
    }

    // Add constraints to constraint group by calling install internally
    [self.constraint activate]; 

    // Refresh the layout
    [self layoutIfNeeded];
}

@end
Copy the code

Method 2: Weak reference constraint

  • It is equivalent to recreating constraints (not all of them, but the ones you want to update) for all situations and can be updated immediately.
@interface xxx(a)
@property (nonatomic.weak) MASConstraint *constraint;
@end

@implementation xxx

- (void)setupConstraints {
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        ......
        self.constraint = make.right.equalTo(self).priorityLow();
    }];
}

- (void)changePriority:(BOOL)isHigh {
    // Invalidate the constraint (internal call uninstall to remove the constraint from the constraint group, because the current constraint is a weak reference, and is not strongly referenced by other Pointers will be recycled)
    [self.constraint uninstall]; 

    // Re-create the constraints (mas_updateConstraints adds the constraints within the block to the constraints group and takes effect)
    [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        if (isHigh) {
            self.constraint = make.right.equalTo(self).priorityHigh();
        } else {
            self.constraint = make.right.equalTo(self).priorityLow(); }}];// Refresh the layout
    [self layoutIfNeeded];
}

@end
Copy the code

Done.