介绍,入门:
http://www.cocoachina.com/ios/20141219/10702.html
下载:
http://code.cocoachina.com/detail/301146/%E8%87%AA%E5%8A%A8%E5%B8%83%E5%B1%80%EF%BC%8CMasonry%E4%BB%8B%E7%BB%8D%E4%B8%8E%E4%BD%BF%E7%94%A8%E5%AE%9E%E8%B7%B5%EF%BC%9A%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8BAutolayout/
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(100, 100));
//居中约束
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(100);
make.left.mas_equalTo(50);
make.size.mas_equalTo(CGSizeMake(100, 100));
}];
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(-50);
}];
}
- (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(20);
make.right.mas_equalTo(-50);
// 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(-20);
//让他等于redBtn的高度
make.height.equalTo(redBtn);
//添加上左约束
make.top.equalTo(redBtn.mas_bottom).offset(0);
make.left.equalTo(weakSelf.view.mas_centerX).offset(-40);
}];
}
- (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 = 10;
// 给左边视图添加约束
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(20);
make.right.equalTo(secondBtn.mas_left).with.offset(-padding1);
}];
// 给右边视图添加约束
[secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.right.mas_equalTo(-20);
make.width.equalTo(firstBtn);
}];
}
- (void)initView4Test {
for (int i = 0; i < 4; i ++) {
UIView * firstBtn = [[UIView alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.size.mas_equalTo(CGSizeMake(50, 50));
make.left.mas_equalTo(i*60);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPressed:)];
firstBtn.tag = 10 +i;
firstBtn.userInteractionEnabled = YES;
[firstBtn addGestureRecognizer:tap];
}
}
- (void)tapPressed:(UITapGestureRecognizer *)sender {
NSInteger index = sender.view.tag;
NSLog(@"%ld",index);
}
@end
// 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(100, 100));
//居中约束
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(100);
make.left.mas_equalTo(50);
make.size.mas_equalTo(CGSizeMake(100, 100));
}];
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(-50);
}];
}
- (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(20);
make.right.mas_equalTo(-50);
// 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(-20);
//让他等于redBtn的高度
make.height.equalTo(redBtn);
//添加上左约束
make.top.equalTo(redBtn.mas_bottom).offset(0);
make.left.equalTo(weakSelf.view.mas_centerX).offset(-40);
}];
}
- (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 = 10;
// 给左边视图添加约束
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.left.mas_equalTo(20);
make.right.equalTo(secondBtn.mas_left).with.offset(-padding1);
}];
// 给右边视图添加约束
[secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.right.mas_equalTo(-20);
make.width.equalTo(firstBtn);
}];
}
- (void)initView4Test {
for (int i = 0; i < 4; i ++) {
UIView * firstBtn = [[UIView alloc]init];
firstBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:firstBtn];
[firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(100);
make.size.mas_equalTo(CGSizeMake(50, 50));
make.left.mas_equalTo(i*60);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPressed:)];
firstBtn.tag = 10 +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/
来源:https://www.cnblogs.com/hxwj/p/4592920.html