前言:

回想起2013年做iOS开发的时候,那时候并没有采用手写布局代码的方式,而是采用xib文件来编写,如果使用纯代码方式是基于window的size(320,480)计算出一个相对位置进行布局,那个时候windows的size是固定不变的,随着iphone5的发布,windows的size(320,568)也发生了变化,而采用autoresizingMask的方式进行适配,到后来iphone 6之后windows size的宽度也随之变化,开始抛弃autoresizingMask改用autolayout了,使用autolayout进行适配我也是最近重新做iOS开发才接触的,公司使用Masonry框架进行布局适配。所以学习使用这个布局框架对我来说至关重要,它大大提高了开发效率而且最近使用起来很多语法和Android有很大的相似之处。

什么是Masonry?

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

如何使用?

1.)引入头文件

我这里是在全局引用pch文件中引用的

#import "Masonry.h"

2.)基本语法

Masonry提供的属性

  • @property (nonatomic, strong, readonly) MASConstraint *left;//左侧
  • @property (nonatomic, strong, readonly) MASConstraint *top;//上侧
  • @property (nonatomic, strong, readonly) MASConstraint *right;//右侧
  • @property (nonatomic, strong, readonly) MASConstraint *bottom;//下侧
  • @property (nonatomic, strong, readonly) MASConstraint *leading;//首部
  • @property (nonatomic, strong, readonly) MASConstraint *trailing;//尾部
  • @property (nonatomic, strong, readonly) MASConstraint *width;//宽
  • @property (nonatomic, strong, readonly) MASConstraint *height;//高
  • @property (nonatomic, strong, readonly) MASConstraint *centerX;//横向居中
  • @property (nonatomic, strong, readonly) MASConstraint *centerY;//纵向居中
  • @property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基线

Masonry提供了三个函数方法

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增约束
  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新约束
  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有约束,只会保留最新的约束

我们根据不同的使用场景来选择使用不同的函数方法。

3.)具体举例

比如一个往父控件中添加一个上下左右与父控件间距为50的子视图

添加约束

    UIView *tempView=[[UIView alloc]init];
tempView.backgroundColor=[UIColor greenColor];
[self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo();
make.right.mas_equalTo(-);
make.top.mas_equalTo();
make.bottom.mas_equalTo(-);
}];

等价于

    UIView *tempView=[[UIView alloc]init];
tempView.backgroundColor=[UIColor greenColor];
[self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset();
make.right.equalTo(self.view.mas_right).offset(-);
make.top.equalTo(self.view.mas_top).offset();
make.bottom.equalTo(self.view.mas_bottom).offset(-);
}];

也可以简化为下面这种

    UIView *tempView=[[UIView alloc]init];
tempView.backgroundColor=[UIColor greenColor];
[self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(, , , ));
}];

又等价于

    UIView *tempView=[[UIView alloc]init];
tempView.backgroundColor=[UIColor greenColor];
[self.view addSubview:tempView]; [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(, , , ));
}];

更新约束

    [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo();
make.right.mas_equalTo(-);
make.top.mas_equalTo();
make.bottom.mas_equalTo(-);
}];

清除之前的约束保留最新的

    [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo();
make.right.mas_equalTo(-);
make.top.mas_equalTo();
make.bottom.mas_equalTo(-);
}];

特别注意:

声明约束必须在视图添加到父试图上面之后调用。

4.)mas_equalTo与equalTo

上面的举例中分别使用了mas_equalTo和equalTo达到了同样的效果,我在刚开始使用Masonry的时候很容易混淆他们两个,今天特意分析一下他们的区别。mas_equalTo是一个MACRO,比较的是值,equalTo比较的是id类型。

总结:

简单记录一下Masonry布局框架的使用。

IOS控件布局之Masonry布局框架的更多相关文章

  1. DevExpress控件GridControl中的布局详解 【转】

    DevExpress控件GridControl中的布局详解 [转] 2012-10-24 13:27:28|  分类: devexpress |  标签:devexpress  |举报|字号 订阅   ...

  2. 【Android】15.0 UI开发(六)——列表控件RecyclerView的网格布局排列实现

    1.0 列表控件RecyclerView的网格布局排列实现,关键词GridLayoutManager. LinearLayoutManager 实现顺序布局 GridLayoutManager 实现网 ...

  3. 【Android】14.0 UI开发(五)——列表控件RecyclerView的瀑布布局排列实现

    1.0 列表控件RecyclerView的瀑布布局排列实现,关键词StaggeredGridLayoutManager LinearLayoutManager 实现顺序布局 GridLayoutMan ...

  4. 设计一个 iOS 控件

    转载自:http://blog.csdn.net/zhangao0086/article/details/45622875 代码的等级:可编译.可运行.可测试.可读.可维护.可复用 前言 一个控件从外 ...

  5. iOS控件之UIResponder类

    iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...

  6. iOS控件——UIView的viewWithTag:(int)findTag方法描述

    UIView拥有一个viewWithTag:(int)findTag方法,调用方式为[MyView viewWithTag:整形数字]该方法返回tag == findTag的控件.ios控件中允许多个 ...

  7. ios学习笔记图片+图片解释(c语言 oc语言 ios控件 ios小项目 ios小功能 swift都有而且笔记完整喔)

    下面是目录其中ios文件夹包括了大部分ios控件的介绍和演示,swift的时完整版,可以学习完swift(这个看的是swift刚出来一周的视频截图,可能有点赶,但是完整),c语言和oc语言的也可以完整 ...

  8. flutter控件之ListView滚动布局

    ListView即滚动列表控件,能将子控件组成可滚动的列表.当你需要排列的子控件超出容器大小,就需要用到滚动块. import 'package:flutter/material.dart'; cla ...

  9. 【Android】13.0 UI开发(四)——列表控件RecyclerView的横向布局排列实现

    1.0 新建项目,由于ListView的局限性,RecyclerView是一种很好取代ListView的控件,可以灵活实现多种布局. 2.0 新建项目RecyclerviewTest,目录如下: 3. ...

随机推荐

  1. openstack controller ha测试环境搭建记录(四)——配置mysql数据库集群

    内容正式开始前,我已经在集群中添加了新的节点controller1(IP地址为10.0.0.14). 在所有节点上安装软件:# yum install -y mariadb-galera-server ...

  2. iOS开发——二级列表

    原理很简单,一级菜单放在viewForHeaderInSection里面,加一个点击事件,然后判断它的二级菜单(cell)显不显示. 直接上代码吧! // //  HeheTableViewContr ...

  3. MySQL show status命令参数

    Aborted_clients         由于客户没有正确关闭连接已经死掉,已经放弃的连接数量.   Aborted_connects 尝试已经失败的MySQL服务器的连接的次数.   Binl ...

  4. struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)

    一.web.xml配置 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...

  5. Thinking in scala (7)---- f(n)=f(n-1)+2f(n-2)+3f(n-3)

    <计算机程序的构造和解释>中的练习1.11: 函数f,如果n<3,那么f(n) = n;如果n>=3,那么 f(n)=f(n-1)+2f(n-2)+3f(n-3) 有了上面的公 ...

  6. win7 下安装 ubuntu 16.04双系统

    Ubuntu 每年发布两个版本,目前最新正式版版本也升到了 16.04.Ubuntu 16.04 开发代号为"Xenial Xerus",为第六个长期支持(LTS)版本,其主要特色 ...

  7. web前端面试第三波~

    快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...

  8. C++中的IO流

    一,标准输入流 1.基本功能(头文件为iostream) char ch = cin.get();// 一次读取一个字符,如果遇到EOF则结束. cin.getline(buf,length);// ...

  9. 用python实现模拟登录人人网

    用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...

  10. 多层神经网络BP算法 原理及推导

    首先什么是人工神经网络?简单来说就是将单个感知器作为一个神经网络节点,然后用此类节点组成一个层次网络结构,我们称此网络即为人工神经网络(本人自己的理解).当网络的层次大于等于3层(输入层+隐藏层(大于 ...