不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐。所以出现了第三方框架。

Masonry 在github地址如下:

  https://github.com/SnapKit/Masonry

如果需要通过代码手动添加约束,Masonry真的是一个不错的选择,大大增加开发效率,何乐而不为呢。

Autolayout - Masonry

  • 使用步骤

    • 添加Masonry文件夹的所有源代码到项目中
    • 添加2个宏、导入主头文件
       // 只要添加了这个宏,就不用带mas_前缀
      #define MAS_SHORTHAND
      // 只要添加了这个宏,equalTo就等价于mas_equalTo
      #define MAS_SHORTHAND_GLOBALS
      // 这个头文件一定要放在上面两个宏的后面
      #import "Masonry.h"
  • 添加约束的方法

 // 这个方法只会添加新的约束
[view makeConstraints:^(MASConstraintMaker *make) { }]; // 这个方法会将以前的所有约束删掉,添加新的约束
[view remakeConstraints:^(MASConstraintMaker *make) { }]; // 这个方法将会覆盖以前的某些特定的约束
[view updateConstraints:^(MASConstraintMaker *make) { }];
  • 约束的类型

    1.尺寸:width\height\size
    2.边界:left\leading\right\trailing\top\bottom
    3.中心点:center\centerX\centerY
    4.边界:edges
  • 示例代码1:居中显示

       // 居中显示
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView]; // 可以使用三个方法来添加约束。
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view);
    make.height.equalTo();
    make.width.equalTo();
    }];
  • 示例代码2:并排位于底部,间距20

      //并排位于底部,间距20
    
       UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView]; UIView *blueView= [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView]; // 添加约束
    [redView makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view.left).offset(); // 左边间距20
    make.right.equalTo(blueView.left).offset(-); // 右边和blueView间距20
    make.bottom.equalTo(self.view.bottom).offset(-); // 底部间距20 make.height.equalTo(); // 高度200 }]; [blueView makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view.right).offset(-); // 右边间距20
    make.bottom.equalTo(redView.bottom); // 和redView底部间距相同 make.height.equalTo(redView); // 等宽
    make.width.equalTo(redView); // 等高
    }];
  • 示例代码3:四个View,平分整个屏幕

 //四个View,平分整个屏幕
//红色
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
// 蓝色
UIView *blueView= [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView];
// 黑色
UIView *blackView = [[UIView alloc] init];
blackView.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackView];
// 绿色
UIView *greenView= [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView]; // autolayout
[redView makeConstraints:^(MASConstraintMaker *make) {
make.left.and.top.equalTo(self.view);
make.right.equalTo(self.view.centerX);
make.bottom.equalTo(self.view.centerY);
}]; [blueView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(redView.right);
make.right.equalTo(self.view);
make.height.equalTo(redView); }]; [blackView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(redView.bottom);
make.height.equalTo(redView);
make.width.equalTo(redView);
}]; [greenView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(blueView.bottom);
make.left.equalTo(blackView.right);
make.height.equalTo(blueView);
make.width.equalTo(blueView);
}]; // 红色View内部
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"010.png"]];
UILabel *name = [[UILabel alloc] init];
name.text = @"红色";
name.textAlignment = NSTextAlignmentCenter;
name.backgroundColor = [UIColor purpleColor];
[redView addSubview:image];
[redView addSubview:name];
[image makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(redView.center).offset(-);
}];
[name makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(redView.left);
make.bottom.equalTo(redView.bottom);
make.height.equalTo();
}];
代码示例4:其他方法使用
  // masonry 自动布局
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; UIView *blueView= [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView]; [redView makeConstraints:^(MASConstraintMaker *make) {
// 大小100*100,居中显示
//make.size.equalTo(100);
//make.center.equalTo(self.view); //居中显示,直接设置距离四面的距离
UIEdgeInsets eda = {,,,};
make.edges.insets(eda);
//
}]; // blueView位于redView中间
[blueView makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(redView);
make.height.equalTo(redView.height).multipliedBy(0.5); // 乘法
make.width.equalTo(redView.width).dividedBy().offset(); // 除法+偏移量
}];

总结:

  和苹果自带的约束添加方法相比,苹果的约束方法简直无法直视啊。这样给控件添加约束简单快捷,主要是条理清晰,言简意赅。



今日如此,明日依旧。
2015-06-04

ios开发学习笔记040-autolayout 第三方框架Masonry的更多相关文章

  1. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  2. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  3. ios开发学习笔记(1)

    objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...

  4. IOS开发学习笔记016-Foundation框架

     Foundation 框架的学习 一.Foundation 常用结构体 1.NSRange(location,length)  typedef struct _NSRange { NSUIntege ...

  5. (ios开发学习笔记一)ios项目文件结构

    转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...

  6. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  7. IOS开发学习笔记017-第一个IOS应用

    第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...

  8. IOS开发学习笔记009-OC基本知识

    开始学习OC,时间不等人啊,要抓紧了. OC基本知识 新建一个以.m结尾的文件,这既是oc的程序文件.在oc程序中完全兼容C语言.编译好链接类似. oc包含头文件是使用#import <> ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:测试SSH框架分层整合及验证事务是否有效

    测试框架分层的整合 HibernateTemplate 和 HibernateDaoSupport,这两个类是 Spring 为整合 Hibernate3 提供的两个工具类. HibernateTem ...

随机推荐

  1. Eucalyptus管理页面密码设置

    桉树环境什么的都已经是配置好了的,但是过了一段时间不用,也不知道密码是什么了,看着下面的页面也不知道如何进去,这里我们通过命令行的方式重置用户名和密码信息. 登陆clc所在机器,输入下命令: euar ...

  2. Jquery里面的$(this)和this, 有什么区别

    当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! "); 这个里的html()是J ...

  3. [AngularJS] $location 服务简介

    参考博客:  https://www.cnblogs.com/gaoruixin/p/6070502.html 简介 $location服务解析在浏览器地址栏中的URL(基于window.locati ...

  4. js 获取时间戳 登陆验证码生成要加时间戳

    JavaScript 获取当前时间戳,登陆验证码生成要加时间戳,防止存在session不重新请求第一种方法: var timestamp = Date.parse(new Date()); 结果:12 ...

  5. 实现带查询功能的ComboBox控件

    实现效果: 知识运用: ComboBox控件的AutoCompleteMode属性 public AutoCompleteMode AutoCompleteMode{get;set;} //属性值为枚 ...

  6. 2017.12.4 JavaWeb中EL表达式的运用

    <%@ page contentType="text/html; charset=gb2312"%> <html> <head> <tit ...

  7. Python求包含数字或字母最长的字符串及长度

    一.求包含数字或字母最长的字符串及长度 org = 'ss121*2222&sdfs2!aaabb' result = [] #保存最终要输出的字符串 result_temp = [] #保存 ...

  8. 小弟在研究CUDA时出现一个问题,求解

    这是<GPU高性能编程CUDA中文实战>中的例子,第七章,热传导模拟,但是出现下面的问题,求牛人解读.小弟跪谢... 主要问题就是关键字变白. 但是添加需要的头文件后一些系统自带的关键字也 ...

  9. C#的接口基础教程之七 覆盖虚接口

    有时候我们需要表达一种抽象的东西,它是一些东西的概括,但我们又不能真正的看到它成为一个实体在我们眼前出现,为此面向对象的编程语言便有了抽象类的概念.C#作为一个面向对象的语言,必然也会引入抽象类这一概 ...

  10. python-的多线程处理

    书到用时方恨少,这句话在软件杯真的深深体会到了.但是对自己对于代码的领会能力还是有自信的,在做项目的时候用到了多线程的处理,开始都不知道该怎么去百度搜索关键词