介绍,入门:

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. JavaWeb之Servlet: ServletConfig 与 ServletContext

    ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...

  2. 包(package) 与 导入(import) 语句剖析

    A) 包(package):用于将完成不同功能的类分门别类,放在不同的目录下. B)命名规则:将公司域名翻转作为包名.例如www.vmaxtam.com域名,那么包名就是com.vmaxtam 每个字 ...

  3. simplexml_load_string获取xml节点里的属性值

    http://stackoverflow.com/questions/14359658/get-xml-attribute-using-simplexml-load-string 问: I am us ...

  4. 自己编写基于MVC的轻量级PHP框架

    做WEB开发已有三年,每次都写重复的东西, 因此,想自己写一下框架,以后开发方便.本人之前asp.NET一年开发,jsp半年,可是后来因为工作的原故换成PHP.其实很不喜欢PHP的语法.还有PHP的函 ...

  5. [转]VC的DDK编译环境构建

    [转]VC的DDK编译环境构建 http://blog.csdn.net/skdev/article/details/1336935   1 环境状况 Windows XP SP1 NTDDK(win ...

  6. 003--VS2013 C++ 多边形绘制

    //全局变量HPEN hPen;HBRUSH hBru;POINT poly1[6], poly2[5], poly3[5]; //---------------------------------- ...

  7. Ubuntu 12.04 Desktop配置XAMPP【转】

    转载:[ubuntu][xampp]开发环境配置 XAMPP 并不适用于生产环境,而仅供开发环境使用.XAMPP 被设置为尽量开放,并提供开发者任何他/她想要的功能.这对于开发环境来说是很棒的,但对于 ...

  8. ActiveX控件的基本操作方法以及如何在VS2010下使用控件

    在此,小编就介绍下ActiveX控件的基本操作方法以及如何在VS2010下使用控件,我们以一个程序为例, (1)      打开VS2010编译器(右键以管理员身份运行,因为ActiveX需要注册), ...

  9. 使用Groovy构建自己的脚本环境

    场景 在进行Web服务端开发的时候,发布前通常需要测试一遍.对于一个大一点的项目,最好的办法是写个自动化测试程序. 以Groovy为例,写测试代码之前通常的有如下几个操作 引用相关的类库 import ...

  10. 阴影 box-shadow(一)

    阴影 box-shadow(一) box-shadow是向盒子添加阴影.支持添加一个或者多个. 很简单的一段代码,就实现了投影效果,酷毙了.我们来看下语法: box-shadow: X轴偏移量 Y轴偏 ...