Masonry自动布局
2015-06-22 09:47
176 查看
介绍,入门:
/article/1225736.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 有待进一步熟悉
补:
自动布局动态计算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/
/article/1225736.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 有待进一步熟悉
补:
自动布局动态计算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/
相关文章推荐
- STM32 SPI总线移植fatfs产生硬件异常问题
- 如果您想省略JS里的分号,了解一下JS的分号插入原理吧
- tar命令的详细解释
- hdoj1078_FatMouse and Cheese(dp)
- android用pull解析xml文件
- AngularJS从构建项目开始
- mysql 分库分表
- Min Stack
- NYIST OJ 题目20 吝啬的王国
- JavaScript创建闭包的两种方式的优劣与区别分析
- 《Artech的WCF之旅系列》系列技术文章整理收藏
- 进程与PCB的关系如何看???
- 仿QQ软件收获
- 关于版本管理的一些初步理解
- 标准差(standard deviation)和标准错误(standard error)你能解释一下?
- 安卓ApiDemos最简单的使用方法
- 在Linux中调度功能是由哪个函数来实现的???
- 进程管理与SELinux初探
- 为什么要引入原语???
- Android中的消息处理实例与分析