Masonry小技巧

作者: zhungxd | 来源:发表于2016-05-18 17:25 被阅读1442次

当你写了一个错误的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更易读。

相关文章

  • Masonry 单个约束的移除(卸载)和安装

    Masonry小技巧门来自 https://github.com/SnapKit/Masonry/issues/1...

  • Masonry小技巧

    当你写了一个错误的constanints,例如: 然后在你会看到如下的debug log: 是不是会眼花,如果有很...

  • Masonry小技巧总结

    一、ScrollView的contentSize 固定内容大小的就没啥好讲了! 下面我们讲下用masonry布局自...

  • Masonry使用的一个小技巧

    今天在Masonry:使用纯代码进行iOS应用的autolayout自适应布局看到了这样一个小技巧,发现之前写的一...

  • masonry使用技巧

    一、基本用法 1、长度关系和位置关系,不能做比例运算。举几个例子我现在希望子视图的横向中心线(centerY)在高...

  • Masonry使用技巧

    一、相同View水平、竖直方向上排布 //- (void)mas_distributeViewsAlongAxis...

  • masonry使用技巧

    masonry git地址:https://github.com/SnapKit/Masonry 本文主要会讲到m...

  • masonry使用技巧

    1.当你初次设置约束时,使用mas_makeConstraints;当你界面的组件的当前约束会变大或者缩小时,就需...

  • Masonry 使用技巧

    Masonry 是一个轻量级的布局框架, 拥有自己的描述语法, 采用更优雅的链式语法封装自动布局 简洁明了 并具有...

  • Masonry小技巧之UIScrollView自适应

    以往我们使用UIScrollView的时候需要设置它的frame和contentSize.其中frame是UISc...

网友评论

本文标题:Masonry小技巧

本文链接:https://www.haomeiwen.com/subject/imywrttx.html