标签:
Masonry链接:https://github.com/SnapKit/Masonry
作用:方便使用代码添加AutoLayout约束(AutoLayout在一定意义上替换了Frame,对Frame的改变变成对约束的操作)
使用步骤:
1、手动添加或者使用Cocoapods添加Masonry框架,并导入头文件Masonry.h
2、将需要添加约束的对象加入到父视图中
3、下面的代码的约束是以父视图为基准点的
以下代码中:使用这种注释方式,可以为属性添加标注,option+左键查看,声明一个Button属性
/** 这是专业的注释写法注释 前面两颗星 后面一颗星*/ /** 按钮1 屏幕右上角 距离上边缘20 右边缘20 宽80 高40 ,颜色红*/ @property (nonatomic,strong) UIButton *redBtn;
以下代码:使用Masonry中的mas_makeConstraints:(void(^)(MASConstraintMaker *make))block给属性添加约束对象,具体约束在block中实现
在block中不要对属性进行操作,所有的约束属性已经全部交给MASConstraintMaker处理,使用make添加约束
单独对控件的上下左右进行约束时,这里规定 “ + ” 号代表向下和向右移动,“ - ” 号代表向上和向左移动
make.size make.top make.left make.bottom make.right
- (UIButton *)redBtn{ if (!_redBtn) { _redBtn = [UIButton buttonWithType:0]; _redBtn.backgroundColor = [UIColor redColor]; #warning 添加约束之前必须把对应控件添加到父视图中 [self.view addSubview:_redBtn]; //使用Masonry的前提条件 pod框架 并 引入Masonry.h文件 [_redBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(80, 40));//当前控件的size //规定 +号代表向下和向右移动,-号代表向上和向左移动 make.top.mas_equalTo(20);// make.right.mas_equalTo(-20); }]; } return _redBtn; }
对属性添加edges约束
/** 视图1 全屏 即四周距离父视图边缘都为0*/ @property (nonatomic,strong) UIView *blueView; /** 视图2 四周都为(20,30,40,50)*/ @property (nonatomic,strong) UIView *orangeView;
edges中的参数,正数表示向中间移动
make.edges.mas_equalTo(UIEdgeInsetsMake(20, 30, 40, 50));
make.edges.mas_equalTo(0);//当边距都为零时,可以用这种简写方式
- (UIView *)orangeView{ if (!_orangeView) { _orangeView = [UIView new]; _orangeView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:_orangeView]; [_orangeView mas_makeConstraints:^(MASConstraintMaker *make) { //edges中的参数 正数表示向中间移动 make.edges.mas_equalTo(UIEdgeInsetsMake(20, 30, 40, 50)); }]; } return _orangeView; } - (UIView *)blueView{ if (!_blueView) { _blueView = [UIView new]; _blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:_blueView]; [_blueView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0);//当边距都为零时,可以用这种简写方式 }]; } return _blueView; }
3、未完,下午继续完成
标签:
原文地址:http://www.cnblogs.com/standup-liang/p/4991278.html