一個輕量級的布局框架, 同時支持 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 的源碼還是值得一看的,有助于提升編程思想,推薦。