介绍,入门:
http://www.cocoachina.com/ios/20141219/10702.html
下载:
1.Masonry初体验:
//
// ViewController.m
// MasonryTest
//
// Created by apple on 15/6/22.
// Copyright (c) 2015年 tqh. All rights reserved.
// #import "ViewController.h"
#import "Masonry.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 防止block中的循环引用
[self initView4];
} - (void)initView1 {
UIButton *button = [[UIButton alloc]init];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
[self Masonry:button];
} //居中约束
- (void)Masonry:(UIView *)view {
__weak typeof (self) weakSelf = self;
[view mas_makeConstraints:^(MASConstraintMaker *make) {
//大小约束
make.size.mas_equalTo(CGSizeMake(, ));
//居中约束
make.center.equalTo(weakSelf.view);
}];
} //固定大小,位置调整 - (void)initView2 {
UIButton * blackBtn = [UIButton new];
blackBtn.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackBtn];
[blackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.left.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(, ));
}];
UIButton * redBtn = [UIButton new];
redBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:redBtn];
[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//使用 and 连接
make.size.and.top.equalTo(blackBtn);
//添加右边距约束(这里的间距是有方向性的,左、上边距约束正数,右、下边距约束为负数)
make.right.mas_equalTo(-);
}];
} - (void)initView3 {
// 防止block中的循环引用
__weak typeof (self) weakSelf = self;
UIButton * redBtn = [[UIButton alloc]init];
redBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:redBtn];
[redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo();
make.right.mas_equalTo(-);
// make.bottom.and.right.mas_equalTo(-20);
}];
UIButton * blueBtn = [UIButton new];
blueBtn.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueBtn];
[blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.and.right.mas_equalTo(-);
//让他等于redBtn的高度
make.height.equalTo(redBtn);
//添加上左约束
make.top.equalTo(redBtn.mas_bottom).offset();
make.left.equalTo(weakSelf.view.mas_centerX).offset(-);
}];
} - (void)initView4 {
// 左边的按键
UIButton * firstBtn = [[UIButton alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
// 右边的按键
UIButton * secondBtn = [[UIButton alloc]init];
secondBtn.backgroundColor = [UIColor blueColor];
[self.view addSubview:secondBtn];
int padding1 = ;
// 给左边视图添加约束
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.left.mas_equalTo();
make.right.equalTo(secondBtn.mas_left).with.offset(-padding1);
}];
// 给右边视图添加约束
[secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.right.mas_equalTo(-);
make.width.equalTo(firstBtn);
}];
} - (void)initView4Test {
for (int i = ; i < ; i ++) { UIView * firstBtn = [[UIView alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo();
make.size.mas_equalTo(CGSizeMake(, ));
make.left.mas_equalTo(i*);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPressed:)];
firstBtn.tag = +i;
firstBtn.userInteractionEnabled = YES;
[firstBtn addGestureRecognizer:tap];
}
} - (void)tapPressed:(UITapGestureRecognizer *)sender {
NSInteger index = sender.view.tag;
NSLog(@"%ld",index);
} @end
有待进一步熟悉
补:
自动布局动态计算cell的高度
http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/
http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/