mas_updateConstraints 没有移除 Masonry 中的约束
Posted
技术标签:
【中文标题】mas_updateConstraints 没有移除 Masonry 中的约束【英文标题】:mas_updateConstraints didn't remove constraint in Masonry 【发布时间】:2015-07-05 03:35:26 【问题描述】:我在我的UITableViewCell
中使用Masonry,该单元格有两个子视图,一个是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
这留下了安装的原始约束,并添加了一个与第一个冲突的新约束。
然后我将第一个约束的分配更改为:
CGFloat labelHeight = label.frame.size.height;
make.height.equalTo(@(labelHeight));
……冲突消失了。
显然,ios Masonry 无法将 UIView
定义的约束更新为 NSNumber
定义的约束。
【讨论】:
以上是关于mas_updateConstraints 没有移除 Masonry 中的约束的主要内容,如果未能解决你的问题,请参考以下文章
QGraphicsScene 没有移除 QWidget 的功能
- (void)applicationWillTerminate:(UIApplication *) 当应用程序从后台移除时应用程序没有调用