新建项目 打开终端 切换到项目目录路径 cd /Users/dc008/Desktop/OC/Masonry
ls查看目录
pod search 第三方框架的名字
如果没有
cuixiaoyu:testCoco dc008$ touch Podfile
cuixiaoyu:testCoco dc008$ open Podfile (在文件里写podfile内容)
新建的项目用 pod install 安装
之前的项目用 pod update 更新
Updating local specs repositories(正在更新)
//
// ViewController.m
// Masonry
//
// Created by dc008 on 16/1/4.
// Copyright © 2016年 lin. All rights reserved.
//
#import "ViewController.h"
#import <Masonry/Masonry.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self test1];
[self test2];
}
- (void)test1{
UIView *view1 = [[UIView alloc]init];
view1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view1];
//添加限制,约束
[view1 mas_makeConstraints:^(MASConstraintMaker *make){
//edges 边
//insets 内边距
//with 没有功能,只是为了可读性更强
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
// make.leftMargin.mas_equalTo(10);
// make.topMargin.mas_equalTo(10);
// make.rightMargin.mas_equalTo(10);
// make.bottomMargin.mas_equalTo(10);
}];
}
- (void)test2{
UIView *view1 = [[UIView alloc]init];
view1.backgroundColor = [UIColor purpleColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc]init];
view2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view2];
UIView *view3 = [[UIView alloc]init];
view3.backgroundColor = [UIColor blueColor];
[self.view addSubview:view3];
//增加约束
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.size.mas_equalTo(CGSizeMake(300, 300));
// make.width.mas_equalTo(100);
// make.left.mas_equalTo(self.view.mas_left).with.offset(10);
// make.centerY.mas_equalTo(self.view.mas_centerY);//让view1的y坐标的中点等于self.view的y坐标中点
make.width.mas_equalTo(view2.mas_width);//宽度等于view2的宽度
make.left.mas_equalTo(self.view.mas_left).with.offset(10);//view1的左边距
make.right.mas_equalTo(view2.mas_left).with.offset(-10);//view1的右边距,相对view2来设置
make.top.mas_equalTo(self.view.mas_top).offset(10);
make.bottom.mas_equalTo(view3.mas_top).offset(-10);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.centerY.mas_equalTo(self.view.mas_centerY);
make.width.mas_equalTo(view1.mas_width);
make.left.mas_equalTo(view1.mas_right).with.offset(10);
make.right.mas_equalTo(self.view.mas_right).with.offset(-10);
make.top.mas_equalTo(self.view.mas_top).offset(10);
make.bottom.mas_equalTo(view3.mas_top).offset(-10);
}];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
// make.centerY.mas_equalTo(self.view.mas_centerY);
make.height.mas_equalTo(view1.mas_height);
make.width.mas_equalTo(self.view.mas_width).offset(-20);
make.top.mas_equalTo(view1.mas_bottom).offset(10);
make.bottom.mas_equalTo(-10);
make.left.mas_equalTo(self.view.mas_left).offset(10);
make.right.mas_equalTo(-10);
}];
//从无到有 显示布局动画
[UIView animateWithDuration:3.0 animations:^{
[self.view layoutIfNeeded];//告知页面布局立刻更新
NSLog(@"%@",NSStringFromCGRect(view1.frame));
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end