用masonry来解决屏幕适配问题,是一个很好的东西,虾面上截图
在使用该工具类的时候
要把该文件集成到cocapods上
1:引入头文件#import "View+MASAdditions.h"
2:对控件添加约束
CGFloat padding = 10;
CGFloat fullButonW = self.contentView.bounds.size.width * 0.5 - padding * 3 ;
CGFloat fullButonH = 40;
UIButton * fullButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fullButton setTitle:@"充值" forState:UIControlStateNormal];
fullButton.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:fullButton];
[fullButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.mas_top).with.offset(10);//当前控件的顶部距离当前父控件的顶部距离为10
make.left.equalTo(self.contentView).with.offset(20);//当前控件的左部距离当前父控件的左部距离为20
make.size.mas_equalTo(CGSizeMake(fullButonW, fullButonH));//设置当前控件的宽和高
}];
这里有几点注意:
1:约束必须写在 [self.contentView addSubview:fullButton];
也就是在addsubview之后,否则会报错关于约束。
3:详细约束信息
make.top.equalTo(self.contentView.mas_top).with.offset(10); //当前控件的顶部距离当前控件父视图的顶部 距离为10
make.left.equalTo(self.contentView).with.offset(20);
make.size.mas_equalTo(CGSizeMake(fullButonW, fullButonH));
make.top.equalTo(custonMoneryLabel.mas_bottom).with.offset(10);//当前控件的顶部距离custonMoneryLabel控件的底部 距离为10
make.left.equalTo(self.contentView.mas_left).with.offset(20); //当前控件的左部距离contentView控件的左部 距离为20
make.right.equalTo(self.contentView.mas_right).with.offset(-20); //当前控件的右部距离contentView控件的右部 距离为-20
make.height.equalTo(@1);//设置当前控件的高位1
make.left.equalTo(self.contentView.mas_centerX).with.offset(20); //当前控件的左部距离contentView控件的中部 距离为20
make.top.equalTo(custonMoneryLabel.mas_bottom).with.offset(30); /当前控件的顶部距离custonMoneryLabel控件的底部 距离为30
- 欢迎各位一块学习,提高逼格!
也可以添加洲洲哥的微信公众号
更多消息
更多信iOS开发信息 请以关注洲洲哥 的微信公众号,不定期有干货推送:
iOS中使用Masonry的简介
于 2015-11-09 17:56:36 首次发布