介绍,入门:

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/C++ 关于大小端模式

    大端模式:  数据的高字节存在低地址  数据的低字节存在高地址 小端模式:  数据的高字节存在高地址  数据的低字节存在低地址 如图,i为int类型占4个字节,但只有1个字节的值为1,另外3个字节值为 ...

  2. Linux ls -l内容详解

    ls -l是列出当前目录下所有文件信息 以下是实例: 具体的文字描述如下: 第1字段:  文件属性字段文件属性字段总共有10个字母组成,第一个字母表示文件类型,如果这个字母是一个减号”-”,则说明该文 ...

  3. TextSwitcher,译为文字转换器控件

    ViewSwitcher仅仅包含子类型TextView.TextSwitcher被用来使屏幕上的label产生动画效果.每当setText(CharSequence)被调用时,TextSwitcher ...

  4. Erlang generic standard behaviours -- gen_server module

    在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...

  5. 解决vs2013不能添加控制器的步骤

    点击:vs2013 更新,更新完以后,再重启下电脑就可以正常使用了

  6. 44.do文件格式

    style1: transcript onif {[file exists rtl_work]} {<span style="white-space:pre"> < ...

  7. Pintos修改优先级捐赠、嵌套捐赠、锁的获得与释放、信号量及PV操作

    Pintos修改优先级捐赠.嵌套捐赠.锁的获得与释放.信号量及PV操作 原有的优先级更改的情况下面没有考虑到捐赠的情况,仅仅只是改变更改了当前线程的优先级,更别说恢复原本优先级了,所以不能通过任何有关 ...

  8. iOS开发HTTPS实现之信任SSL证书和自签名证书

    iOS开发HTTPS实现之信任SSL证书和自签名证书 转自:http://www.jianshu.com/p/6b9c8bd5005a/comments/5539345 (收录一下供自己学习用的) 字 ...

  9. C++ this指针详解

       C++this指针操作 在这里总结一下this 指针的相关知识点. 首先,我们都知道类的成员函数可以访问类的数据(限定符只是限定于类外的一些操作,类内的一切对于成员函数来说都是透明的),那么成员 ...

  10. python-day3-集合

    集合的特性:无序性,唯一性,可嵌套性 1 #创建集合方式 2 s1={11,22}# 直接创建 3 s2=set()#创建空集合 4 s3=set([111,222,333])#转换为集合 1 #集合 ...