Masonry小技巧

当你写了一个错误的constanints,例如:

[self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(15.0f);
make.left.equalTo(self.contentView).offset(10.f);
make.height.equalTo(10);
make.bottom.equalTo(self.contentView);
}];

然后在你会看到如下的debug log:

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:0x1704a9b40 UILabel:0x12fde1c10.top == UITableViewCellContentView:0x12fdde840.top + 15>",
"<MASLayoutConstraint:0x1704a9c00 UILabel:0x12fde1c10.height == 10>",
"<MASLayoutConstraint:0x1704a9c60 UILabel:0x12fde1c10.bottom == UITableViewCellContentView:0x12fdde840.bottom>",
"<NSLayoutConstraint:0x17429fae0 UITableViewCellContentView:0x12fdde840.height == 177.5>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x1704a9c00 UILabel:0x12fde1c10.height == 10>
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.

是不是会眼花,如果有很多label你是不是需要找半天。
现在你只需要加上一句

self.statusLabel = [UILabel new];
[self.contentView addSubview:self.statusLabel];
MASAttachKeys(self.statusLabel);

只要加一句,不需要¥998

然后log会变成这样

(
"<MASLayoutConstraint:0x1704a4980 UILabel:self.statusLabel.top == UITableViewCellContentView:0x147f046f0.top + 15>",
"<MASLayoutConstraint:0x1704a4a40 UILabel:self.statusLabel.height == 10>",
"<MASLayoutConstraint:0x1704a4aa0 UILabel:self.statusLabel.bottom == UITableViewCellContentView:0x147f046f0.bottom>",
"<NSLayoutConstraint:0x170496080 UITableViewCellContentView:0x147f046f0.height == 177.5>"
)
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x1704a4a40 UILabel:self.statusLabel.height == 10>

是不是很清楚,一看就知道错在哪里。

同理,如果你没有用Masonry,而只是用系统的autolayout API,可以试试NSLayoutConstraint的identifier来让你的debug log更易读。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、ScrollView的contentSize 固定内容大小的就没啥好讲了! 下面我们讲下用masonry布局自...
    _Boring阅读 513评论 0 1
  • Autolayout就像一个知情达理,善解人意的好姑娘,可惜长相有点不堪入目,所以追求者寥寥无几。所幸遇到了化妆大...
    小笨狼阅读 24,013评论 28 227
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,192评论 4 61
  • 本文内容全部转载自追求Masonry 目录 『使用』 一、MASConstraintMaker二、MASConst...
    Vinc阅读 3,551评论 4 18
  • 一切都想舒适美好,一切都想实现。
    光锋容阅读 120评论 0 0