Masonry集锦
有没有处女座的,有没有完美工作者,有没有见不得警告和log的编程爱好者。
使用masory进行布局时,加载UITableViewCell 或者UICollectionViewCell出现log:
log1:
[LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of the contentView of a UITableViewCell is not supported and will result in undefined behavior, as this property is managed by the owning UITableViewCell. Cell: <***TableViewCell: 0x15e0cca00; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x1c042bf40>>
解决办法:
将需要加载的view设置在cell上,不要设置在contentView。在设置masonry时,也不要进行设置self.contentView的约束。即
[self addSubview:self.needLoadView];
[self.needLoadView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(5);
make.left.equalTo(self).offset(5);
make.right.equalTo(self).offset(-5);
make.bottom.equalTo(self).offset(-5);
}];
效果同样实现,且不会出现log输出。
网上有很多说进行设置autoresizingMask或者translatesAutoresizingMaskIntoConstraints,然而经过实践,然并卵。
log2:
**部分片段如下
[LayoutConstraints] 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.
<MASLayoutConstraint:0x1c44b1640 UILabel:0x1021e9970.bottom == **Cell:0x102965e00.bottom - 10>,
<NSLayoutConstraint:0x1c429b260 **Cell:0x102965e00.height == 44>,
Will attempt to recover by breaking constraint
<MASLayoutConstraint:0x1c44b1520 UILabel:0x1021e9970.top == UILabel:0x1021ec6d0.bottom + 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.
这里的原因就很简单了,
第一种,你的masonry布局有问题,这种的情况很少。因为布局有问题界面就会直接显示出来,大家都这么厉害了,肯定不会是这种的情况。
第二种,主要是因为使用masonry之后,在tableview中就很少会去设置cell高度,而是自动计算。这时候log的44就是默认高度,而计算出来的和默认的有冲突,虽然不会影响展示,但是会log。我们遇到的大多数都是这种情况,此种的解决办法是:
在设置bottom的时候添加一个优先权设置。
设置下 .priority(value),如下:
make.bottom.equalTo(self).offset(-10).priority(400);
实践出真知,或许还有别的情况,大家发现了可以再讨论一下。