2

我在我的UITableViewCell中使用Masonry,單元格有兩個子視圖,一個是contentLabel,另一個是imageView。 雖然imageView並不總是存在,但如果單元格有一個圖像url,顯示它或隱藏。如果imageView是隱藏的,我想給contentLabel設置爲cell.contentView.bottom另一個值,東西象下面這樣:mas_updateConstraints沒有去除砌體中的約束條件

[_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沒」刪除舊的約束,但添加一個新的約束,兩者相互衝突。那麼我怎麼能在運行時動態地更新約束值呢?

回答

0

我什麼錯在這裏是我錯過了有關的XCode的行爲的知識,當我清除Xcode的構建緩存,砌體性能好。

+0

面臨着同樣的問題,明確不幫:( – trickster77777

+0

同樣在這裏,我必須存放在約束屬性進行更新和「更新」之前卸載它。如果不卸載,我有兩個相互矛盾的制約。一個乾淨的構建呢沒有幫助。 –

0

也許這不是你的問題,但它可能會幫助別人:

我的問題基本上是以下幾點:

我指派的第一高度約束的UIView一個子類:

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

後來,我試圖用「更新」這個限制:

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

這會保留安裝的原始約束,並添加與第一個約束衝突的新約束。

我再變1約束的分配:

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

......和衝突不見了。

顯然,iOS Masonry無法通過由NSNumber定義的約束更新由UIView定義的約束。