mas_updateConstraints не удалил ограничение в масонстве

Я использую масонство в моем UITableViewCell, у ячейки есть два подпредставления, одно contentLabelдругой imageView, В то время как imageView Не всегда присутствует, если ячейка имеет один URL-адрес изображения, представляет его или скрывает. Если imageView скрыт, я хочу установить contentLabel в cell.contentView.bottom в другое значение, что-то вроде ниже:

  [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView.mas_left);
    make.top.equalTo(self.contentView.mas_bottom).offset(20);
    make.right.equalTo(self.contentView.mas_right).offset(-6);
    _bottomConstraint = make.bottom.equalTo(_imageView.mas_top).offset(-14);
  }];

  [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_contentLabel.mas_left);
    make.bottom.equalTo(self.contentView.mas_bottom).offset(-14);
  }];

  if (tweet.imageUrl) {
    _imageView.hidden = NO;
    [_imageView sd_setImageWithURL:tweet.imageUrl placeholderImage:[UIImage imageNamed:@"loading"] options:0];
  } else {
    _imageView.hidden = YES;
    [_imageView sd_setImageWithURL:NULL];
  }

  if (_imageView.hidden) {
    [_contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
      make.bottom.equalTo(_imageView.mas_top).offset(0);
    }];
  }

Но я всегда получаю сообщение об ошибке ниже:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<MASLayoutConstraint:0x7fef9cd178d0 UILabel:0x7fef9cd437e0.bottom == UIImageView:0x7fef9cd26260.top - 14>",
    "<MASLayoutConstraint:0x7fef9caf5430 UILabel:0x7fef9cd437e0.bottom == UIImageView:0x7fef9cd26260.top>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x7fef9caf5430 UILabel:0x7fef9cd437e0.bottom == UIImageView:0x7fef9cd26260.top>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Кажется mas_updateConstraints не удаляли старое ограничение, но добавляли новое ограничение, оба конфликтовали друг с другом. Итак, как я могу динамически обновлять значение ограничения в зависимости от времени выполнения?

1 ответ

Может быть, это не ваша проблема, но это может помочь другим:

По сути, моя проблема заключалась в следующем:

Я назначил сначала ограничение высоты для подкласса UIView:

make.height.equalTo(label); // label is another subclass of UIView

Позже я попытался "обновить" это ограничение, используя:

make.height.equalTo(@(newHeight)); // newHeight is a CGFloat  

Это оставило оригинальное ограничение установленным, и добавило новое ограничение, которое противоречило 1-му.

Затем я изменил назначение 1-го ограничения на:

CGFloat labelHeight = label.frame.size.height;
make.height.equalTo(@(labelHeight)); 

... и конфликт исчез.

Очевидно, iOS Masonry не может обновить ограничение, определенное UIView ограничением, определяемым NSNumber,

Что я здесь не так, так это то, что мне не хватало знаний о поведении XCode, когда я очищаю кеш сборки XCode, производительность масонства хорошая.

Другие вопросы по тегам