2

セルには 2 つのサブビューがあり、1 つは、もう1 つはです。は常に存在するとは限りませんが、セルに 1 つの画像 URL がある場合は、それを表示するか非表示にします。が非表示の場合、toを別の値に設定したい場合は、次のようになります。UITableViewCellcontentLabelimageViewimageViewimageViewcontentLabelcell.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は古い制約を削除しなかったようですが、新しい制約を追加しました.2つは互いに競合していました。では、ランタイムに基づいて制約値を動的に更新するにはどうすればよいでしょうか?

4

1 に答える 1

1

これはあなたの問題ではないかもしれませんが、他の人を助けるかもしれません:

私の問題は本質的に次のとおりでした。

最初に高さの制約を のサブクラスに割り当てましたUIView:

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

後で、次を使用してこの制約を「更新」しようとしました。

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

これにより、元の制約がインストールされたままになり、最初の制約と競合する新しい制約が追加されました。

次に、最初の制約の割り当てを次のように変更しました。

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

…そして紛争はなくなりました。

どうやら、iOS Masonry は、 によって定義された制約によって、 によって定義された制約を更新できませUIViewNSNumber

于 2016-03-10T19:48:19.470 に答える