介绍,入门:

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(, ));
        
        //居中约束
        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/

Masonry自动布局的更多相关文章

  1. Masonry自动布局使用

    Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL.简洁明了并具有高可读性 而且同时支持 iOS 和 Max OS X. 下载 NSLayoutConstraint ...

  2. Masonry自动布局与UIScrolView适配

    Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...

  3. Masonry自动布局:复合约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  4. IOS Masonry自动布局

    之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错. #import "Masonry.h" #i ...

  5. 【iOS】Masonry 自动布局 MASViewConstraint.m:207 错误

    问题详情: Assertion failure 报错原因: make.right.equalTo([_imageView superview]).right.with.offset(-); make. ...

  6. Coding源码学习第四部分(Masonry介绍与使用(三))

    接上篇继续进行Masonry 的学习. (12)tableViewCell 布局 #import "TableViewController.h" #import "Tes ...

  7. masonry使用问题

    2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...

  8. iOS masonry 不规则tagView布局 并自适应高度

    在搜索页面经常会有不规则的tag出现,这种tagView要有点击事件,单个tagView可以设置文字颜色,宽度不固定根据内容自适应,高度固定,数量不固定.总高度就不固定.最近对于masonry的使用又 ...

  9. Masonry学习札记

    Masnory学习札记 在之前的文章里有草草提到过Masonry自动布局,可这么重要第三方布局框架的怎么可以怎么随便带过呢!昨天在完成页面的时候刚好遇到了被Masorny功能惊叹的部分,所以趁热打铁写 ...

随机推荐

  1. 博客导出工具(C++实现,支持sina,csdn,自定义列表)

    操作系统:windowAll 编程工具:visual studio 2013 编程语言:VC++ 最近博文更新的较频繁,为了防止账号异常引起csdn博文丢失,所以花了点时间做了个小工具来导出博文,用做 ...

  2. ED/EP系列5《消费指令》

    1. 消费交易 消费交易允许持卡人使用电子存折或电子钱包的余额进行购物或获取服务. 特点: 1) --可以在销售点终端(POS)上脱机进行 2) --使用电子存折进行的消费交易必须提交个人识别码(PI ...

  3. jQuery的筛选选择器

    基本筛选选择器 很多时候我们不能直接通过基本选择器与层级选择器找到我们想要的元素,为此jQuery提供了一系列的筛选选择器用来更快捷的找到所需的DOM元素.筛选选择器很多都不是CSS的规范,而是jQu ...

  4. Spring组件扫描<context:component-scan/>使用详解

    1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...

  5. React Native相关

    安装相关工具参考(视频):http://ninghao.net/course/3001?a=26 学习参考:http://reactnative.cn/ 学习参考:http://www.ruanyif ...

  6. hdu 1196 Lowest Bit

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1196 Lowest Bit Description Given an positive integer ...

  7. hdu 4417 Super Mario/树套树

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...

  8. 2天驾驭DIV+CSS (实战篇)(转)

     这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是实战篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...

  9. golang的nil

    golang中什么样的类型可以赋值nil? 类型文档中有注定"uninitialized value is nil"的类型都可以赋值nil. golang的基本类型不能赋值nil: ...

  10. JVM学习总结五(番外)——VisualVM

    距离上次介绍Jconsole已经时隔两周了,这期间由于工作中要用go来做一个新项目,所以精力都用在入门go上了,不过发现go语言用起来真的挺不错的,比python感觉还好点,大家没事可以了解下.   ...