iOS开发swift是未来iOS Developer

一点Masonry的使用技巧

2016-02-05  本文已影响543人  151ba12c22e5

  先假设一个场景。在cell复用的时候,有一个UILable的位置是需要根据数据来确定这个label是距离cell的top 10个像素,还是距离cell的bottom10个像素。有了这个需求,我们先来想几个备选方案。

1、重写layoutSubviews每次都重写label的mas_updateConstraints

make.top.equalTo(self.mas_top).offset(10)

或是

make.bottom.equalTo(self.mas_bottom).offset(-10)(根本行不通)

2、重写layoutSubviews每次都重写label的mas_remakeConstraints然后重新写布局(性能有影响)

ok,说一下我的解决方案。

在init方法里先写好一种无论是top还是bottom,同时把这个约束记录下来

@property(nonatomic,strong)MASConstraint*labelConstraint;

[self.tipLabelmas_makeConstraints:^(MASConstraintMaker*make) {

make.left.equalTo(self.mas_left).offset(20);

make.right.equalTo(self.mas_right).offset(-20);

self.labelConstraint= make.top.equalTo(self.mas_top).offset(10);

}];

好了,重点来了。

每次先卸载top或是bottom的布局(并不清楚卸载这次词用的对不对),然后再重写布局。举一反三这个小技巧可以解决很多问题

恬不知耻的放一波github  https://github.com/timelessg

上一篇下一篇

猜你喜欢

热点阅读