一个轻量级的布局框架, 同时支持 iOS 和 Mac OS X, 采用更优雅的链式语法封装自动布局,语法优雅, 帮助开发者快速适配不同分辨率的 iOS 设备
附上地址 https://github.com/SnapKit/Masonry
singleView
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
//先加到 view 上,在用 masonry 写约束
//添加约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 方法一
// make.left.equalTo(@20);
// make.top.equalTo(@50);
// make.right.equalTo(@-100);
// make.bottom.equalTo(@-200);
// -100,-200 相对数学中的 xy 坐标系来想就想的明白了
// 方法二
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 20, 200, 100));
// UIEdgeInsets insets = {top, left, bottom, right};
}];
TwoViews
-(void)twoViews
{
UIView *view1 = [[UIView alloc]init];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor yellowColor];
//在做约束之前,需要先把view添加到父视图上面
[self.view addSubview:view1];
[self.view addSubview:view2];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@10);
make.top.equalTo(@100);
make.height.equalTo(@150);
}];
//view1的 width 是根据 view2来定的,所以不能写死
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(view1.mas_right).offset(10);
//offset view2相对于 view1的偏移量
make.right.equalTo(@-10);
make.top.equalTo(view1.mas_top);
make.height.equalTo(view1);
make.width.equalTo(view1);
//宽度和高度是size
// make.size.equalTo(view1);
}];
ScrollView
-(void)scrollView
{
UIView *backView = [[UIView alloc]init];
backView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:backView];
[backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 10, 100, 100));
}];
UIScrollView *scroll = [[UIScrollView alloc]init];
[backView addSubview:scroll];
scroll.backgroundColor = [UIColor orangeColor];
[scroll mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(backView);
}];
scroll.contentSize = CGSizeMake(0, 1000);
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor blueColor];
[scroll addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(@10);
// make.right.equalTo(@-10);
// make.top.equalTo(@100);
// make.bottom.equalTo(@-50);
make.edges.equalTo(scroll);
// make.size.equalTo(backView);
make.width.equalTo(scroll);
// make.height.equalTo(scroll);
}];
UIView *lastView;
for (int i = 0; i<5; i++) {
UIView *subview = [[UIView alloc]init];
subview.backgroundColor = [UIColor colorWithRed:(arc4random()%7)*0.1 green:(arc4random()%7)*0.1 blue:(arc4random()%7)*0.1 alpha:1.0];
[view addSubview:subview];
if (i == 0) {
[subview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@20);
make.top.equalTo(@20);
make.right.equalTo(@-20);
make.height.equalTo(@(i * 100 + 100));
}];
}
else
{
[subview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(lastView);
make.width.equalTo(lastView);
make.height.equalTo(@(i * 100 + 100));
make.top.equalTo(lastView.mas_bottom).offset(10);
}];
}
lastView = subview;
}
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom).offset(20);;
}];
}
6出来了以后 AutoLayout 也是一种不错的解决方案,masonry 的源码还是值得一看的,有助于提升编程思想,推荐。