// // ViewController.m // Masonry適配 // // Created by asun on 16/5/24. // Copyright © 2016年 wuchang. All rights reserved. // #import "ViewController.h" #import <Masonry.h> @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