介绍,入门:

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. DB2递归查询

    斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n&g ...

  2. FPGA内部信号避免高阻态

    RT,否则警告Warning: Tri-state node(s) do not directly drive top-level pin(s),会利用或门代替中间的扇出fan-out. 原因:在进行 ...

  3. 读:HIS 与医保系统的接入方案及实现

    HIS 与医保系统的接入方案及实现刘剑锋 李刚荣第三军医大学西南医院信息科(重庆 400038) 医院HIS和医保系统的接口设计方案涉及两个部分,分别由医院和医保中心分别完成相,应的程序设计,这两部分 ...

  4. 【工作总结】LLDB调试技巧 - 篇一

    备忘命令 : 命令“l”可以查看程序当前运行的位置 (lldb) l } - (void)rightBarButtonAction { 命令“bt”也能查看程序运行的调用栈 (lldb) bt * t ...

  5. 标准非STL之bitset

    template <size_t N> class bitset; BitsetA bitset stores bits (elements with only two possible ...

  6. DiskFileItemFactory类---分析及运用

    DiskFileItemFactory类 将请求消息实体中的每一个项目封装成单独的DiskFileItem (FileItem接口的实现) 对象的任务由 org.apache.commons.file ...

  7. sqlserver2008r2 127.0.0.1 用户sa登录失败 错误18456

    按照网上的所有方法都试过了,还是不行. 最后,将sa密码重新设置一下,解决问题.

  8. 《我是IT小小鸟》读书笔记

    转眼间,大学的第二学期悄悄来临了,老师给我们布置了一道原本我以为很无趣的题目----写<我是IT的读书笔记>,但是我读了<我是IT小小鸟>这本书后,令我受益匪浅:五个人,每个人 ...

  9. codeforce 421D D. Bug in Code

    题目链接: http://codeforces.com/problemset/problem/421/D D. Bug in Code time limit per test 1 secondmemo ...

  10. Eclipse支持Jquery代码提示(JqeuryWTP)

    问题描述:        Eclipse支持Jquery代码提示   问题解决: 下载 JqueryWTP.jar文件         文件替换        在Eclipse/plugin 路径下, ...