Masonry是一个很好的三方库,给iOS开发者提供了很多方便,基本使用大家都很熟悉。但在日常开发中,我发现Masonry有一些被忽略的点,但又很实用,在这里分享一下。
一.子view撑开父view
对于文字的自适应宽度很适合
红色的oneView宽被黄色子view撑开
屏幕快照 2019-04-20 22.55.09.png
```
//子view撑开父view 子view要把相对父view的所有约束都写完整
self.onePView = [[UIView alloc]init];
[self.view addSubview:self.onePView];
[self.onePView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.height.mas_equalTo(100);
make.top.equalTo(self.view).offset(90);
}];
self.onePView.backgroundColor = [UIColor redColor];
self.oneSView = [[UIView alloc]init];
[self.onePView addSubview:self.oneSView];
[self.oneSView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(self.onePView);
make.height.mas_equalTo(100);
make.width.mas_equalTo(100);
make.right.equalTo(self.onePView.mas_right).offset(-10);
}];
self.oneSView.backgroundColor = [UIColor yellowColor];
``
二.//设置约束的边界值 可以设置MASViewAttribute和NSNumber
文字设置最大宽度
屏幕快照 2019-04-20 22.58.11.png
self.fourSubLabel = [[UILabel alloc]init];
[self.view addSubview:self.fourSubLabel];
[self.fourSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.oneSView.mas_bottom).offset(20);
make.height.mas_equalTo(20);
make.width.lessThanOrEqualTo(@80);
}];
self.fourSubLabel.text = @"fffffffffffffffff";
self.fourSubLabel.backgroundColor = [UIColor greenColor];
self.fiveSubLabel = [[UILabel alloc]init];
[self.view addSubview:self.fiveSubLabel];
[self.fiveSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.fourSubLabel.mas_bottom).offset(5);
make.height.mas_equalTo(20);
}];
self.fiveSubLabel.text = @"fffffffffffffffff";
self.fiveSubLabel.backgroundColor = [UIColor greenColor];
self.sixSubLabel = [[UILabel alloc]init];
[self.view addSubview:self.sixSubLabel];
[self.sixSubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.fiveSubLabel.mas_bottom).offset(5);
make.height.mas_equalTo(20);
make.right.greaterThanOrEqualTo(self.fourSubLabel.mas_right);
}];
self.sixSubLabel.text = @"ffff ";
self.sixSubLabel.backgroundColor = [UIColor greenColor];
网友评论