//// ViewController.m// Masonry适配//// Created by asun on 16/5/24.// Copyright © 2016年 wuchang. All rights reserved.//#import "ViewController.h"#import@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; CGFloat width = WIDTH / 5.0; UIView *view = [[UIView alloc]init]; view.backgroundColor = [UIColor yellowColor]; [self.view addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.view);//屏幕居中 make.width.and.height.mas_equalTo(width); }]; UILabel *label1 = [[UILabel alloc]init]; label1.backgroundColor = [UIColor redColor]; [self.view addSubview:label1]; [label1 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(view); //X轴在控件view中居中显示 make.top.equalTo(view).offset(JWScaleY(10)); //距离顶部 make.bottom.equalTo(view).offset(JWScaleY(-30));//距离底部 (底部和右侧都是负数) make.width.equalTo(view).offset(view.width - JWScaleY(40));//宽度 }]; UILabel *label2 = [[UILabel alloc]init]; label2.textAlignment = NSTextAlignmentCenter; label2.text = @"label2"; [self.view addSubview:label2]; [label2 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(label1.mas_bottom).offset(0); //距离lab1底部0像素 make.width.mas_equalTo(width); make.centerX.equalTo(view); //X轴在控件view中居中显示 make.bottom.equalTo(view); //label2底部和view的底部重叠 }]; }- (void) layout{ UIView *views = [[UIView alloc]init]; views.backgroundColor = [UIColor blackColor]; [self.view addSubview:views]; //在做适配之前需要先把控件加到视图上,要不然会报错 __weak typeof(self)weakSelf = self; [views mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view); make.size.mas_equalTo(CGSizeMake(300, 300)); }]; UIView *view1 = [[UIView alloc]init]; view1.backgroundColor = [UIColor yellowColor]; [views addSubview:view1]; [view1 mas_makeConstraints:^(MASConstraintMaker *make) { // make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); //或者 // make.top.equalTo(view).offset(10); // make.left.equalTo(view).offset(10); // make.right.equalTo(view).offset(-10); // make.bottom.equalTo(view).offset(-10); //在或者 make.top.left.right.bottom.equalTo(weakSelf.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10)); }]; UIView *view2 =[[UIView alloc]init]; view2.backgroundColor = [UIColor redColor]; [view1 addSubview:view2]; UIView *view3 = [[UIView alloc]init]; view3.backgroundColor = [UIColor blueColor]; [view1 addSubview:view3]; [view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(view1).offset(10); make.left.equalTo(view1).offset(10); make.bottom.equalTo(view1).offset(-10); make.width.mas_equalTo(100); }]; [view3 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view2.mas_right).offset(10);//设置view3的左侧距离view2的右侧10 make.top.equalTo(view1).offset(10); make.right.equalTo(view1).offset(-10); make.bottom.equalTo(view1).offset(-10); }];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end