随着iPhone手机屏幕尺寸越来越多样化,在开发过程中的屏幕适配工作也变的越来越重要。

但是系统自带的autolayout,代码繁琐。

Masonry是一个轻量级的布局框架,用更加简洁的语法重新描述了自动布局,并且具有更高可读性。

masonry所支持的属性有:

练习1:

 1     - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4
 5     UIView *view1 = [[UIView alloc] init];
 6     view1.backgroundColor = [UIColor redColor];
 7     [self.view addSubview:view1];
 8     // 给view1添加约束条件
 9    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
10        make.center.equalTo(self.view);
11        make.size.mas_equalTo(CGSizeMake(300, 300));
12    }];
13 }

结果如下图所示:

练习2:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4
 5     UIView *view1 = [[UIView alloc] init];
 6     view1.backgroundColor = [UIColor redColor];
 7     [self.view addSubview:view1];
 8     // 给view1添加约束条件
 9    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
10        make.center.equalTo(self.view);
11        make.size.mas_equalTo(CGSizeMake(300, 300));
12    }];
13
14     UIView *view2 = [[UIView alloc] init];
15     view2.backgroundColor = [UIColor yellowColor];
16     [view1 addSubview:view2];
17     // 给view2添加约束
18     [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
19 //        make.center.equalTo(view1);
20         make.edges.equalTo(view1).with.insets(UIEdgeInsetsMake(10, 20, 40, 60));
21
22     }];
23
24
25 }

结果如下:(设置黄色view的上左下右的内边距分别为10、20、40、60)

练习3:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3
 4     int padding = 10;
 5
 6     UIView *view1 = [[UIView alloc] init];
 7     view1.backgroundColor = [UIColor redColor];
 8     [self.view addSubview:view1];
 9
10     UIView *view2 = [[UIView alloc] init];
11     view2.backgroundColor = [UIColor yellowColor];
12     [self.view addSubview:view2];
13
14
15     // 给view1添加约束条件
16     [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
17         make.centerY.mas_equalTo(self.view.mas_centerY);
18         make.left.equalTo(self.view.mas_left).with.offset(padding);
19         make.right.equalTo(view1.mas_left).with.offset(-padding);
20         make.height.mas_equalTo(@100);
21         make.width.equalTo(view2);
22
23     }];
24
25
26     // 给view2添加约束
27     [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
28         make.centerY.equalTo(self.view.mas_centerY);
29         make.left.equalTo(view1.mas_right).with.offset(padding);
30         make.right.equalTo(self.view.mas_right).with.offset(-padding);
31         make.height.equalTo(@100);
32         make.width.equalTo(view1);
33
34
35     }];
36
37 }

结果如下:通过添加约束自动计算红色和黄色view的宽度

练习4:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3
 4     // 在scrollView中顺序排列一些view并计算contentSize
 5     UIScrollView *scrollView = [[UIScrollView alloc] init];
 6     scrollView.backgroundColor = [UIColor yellowColor];
 7     [self.view addSubview:scrollView];
 8
 9     // 给scrollView添加约束条件
10     [scrollView makeConstraints:^(MASConstraintMaker *make) {
11         make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
12
13     }];
14
15     UIView *container = [[UIView alloc] init];
16     [scrollView addSubview:container];
17
18     // 给container 添加约束条件
19     [container makeConstraints:^(MASConstraintMaker *make) {
20         make.edges.equalTo(scrollView);
21         make.width.equalTo(scrollView);
22     }];
23
24     int count = 10;
25     UIView *lastView = nil;
26     for (int i = 1; i <= count; i++) {
27         UIView *subView = [[UIView alloc] init];
28         [container addSubview:subView];
29
30         subView.backgroundColor = SHRandomColor;
31
32         [subView makeConstraints:^(MASConstraintMaker *make) {
33             make.left.and.right.equalTo(container);
34             make.height.equalTo(@(20 * i));
35             if (lastView) {
36                 make.top.equalTo(lastView.bottom);
37             }else{
38                 make.top.equalTo(container.top);
39             }
40
41         }];
42         lastView = subView;
43     }
44
45
46     [container makeConstraints:^(MASConstraintMaker *make) {
47         make.bottom.equalTo(lastView.bottom);
48     }];
49 }

参考:

http://www.cocoachina.com/ios/20141219/10702.html

转载于:https://www.cnblogs.com/SH9186ios/p/4671173.html

04 - Masonry使用练习相关推荐

  1. 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)

    [jQuery插件]用jQuery Masonry快速构建一个pinterest网站布局 时间:2011年03月21日作者:愚人码头查看次数:29,744 views评论次数:25条评论 前段时间领导 ...

  2. 一个 bug / Masonry的引入

    支持iOS 6/6 Plus的时候碰到这样一个问题: 塞在UITableViewCell里的UIPageControl居中设置失败了,偏右 刚刚设置时下断点查看(6Plus模拟器): <Gray ...

  3. 从Masonry 源码探 Objective-C [源码学习开篇]

    原文链接 : http://blog.csdn.net/sinat_30162391/article/details/53321046 前言: 这个系列可能不会分析, Masonry如何如何好, 估计 ...

  4. docker 配置使用宿主机的GPU(ubuntu16.04+cuda10.0+cudnn7)

    1. 安装 Docker 卸载旧版本 Docker sudo apt-get remove docker docker-engine docker.io containerd runc 安装新版本 s ...

  5. Ubuntu 16.04 安装后修改屏幕分辨率(xrandr: Failed to get size of gamma for output default)

    ubuntu 16.04 安装后分辨率只有一个选项 1024x768,使用 xrandr 命令出现错误: xrandr: Failed to get size of gamma for output ...

  6. Ubuntu 16.04 安装 Docker - Dependency failed for Docker Application Container

    Docker 安装 由于 apt 官方库里的 Docker 版本可能比较旧,所以先卸载可能存在的旧版本: sudo apt-get remove docker docker-engine docker ...

  7. 【Docker】Ubuntu18.04国内源安装Docker-准备工作(一)

    前言: 安装docker由于很多教程都使用国外源和阿里源,安装失败,这里总结一种国内源的安装方法,亲测有效! 过程: 步骤1:在服务器上创建虚拟机 远程连接服务器,win+R--输入mstsc---- ...

  8. 在Ubuntu18.04上安装opencv 3.4.1

    对于安装opencv有的人一次就成功,而有人安装了N多次才成功.我就是那个安装了N多次的人,每次遇到了很多安装错误,只能通过到网上搜教程资料,解决方法:通过一次次的试错,最终完成了安装.再此提醒第一次 ...

  9. Go 中 time.Parse 报错:year/month/day hour/minute/second out of range 时间格式化为什么是 2006-01-02 15:04:05?

    1. 问题现象 在使用 Go 语言的 time.Parse 解析时间时遇到以下错误: func main() {timeParse, err := time.Parse("2006-11-0 ...

  10. Go 学习笔记(25)— 并发(04)[有缓冲/无缓冲通道、WaitGroup 协程同步、select 多路监听通道、close 关闭通道、channel 传参或作为结构体成员]

    1. 无缓冲的通道 无缓冲的通道(unbuffered channel)是指在接收前没有能力保存任何值的通道. 这种类型的通道要求发送 goroutine 和接收 goroutine 同时准备好,才能 ...

最新文章

  1. 报名 | 美团是怎样给你推荐外卖的?美团大脑知识图谱详解
  2. 第一门编程语言的选择无关紧要?
  3. DNS通道检测 国外学术界研究情况——研究方法:基于流量,使用机器学习分类算法居多,也有使用聚类算法的;此外使用域名zif low也有...
  4. 城联数据TSM技术方案起底
  5. c++-内存管理-G4.9
  6. Lambda表达式的标准格式【理解】
  7. npm i依赖版本兼容问题处理
  8. struts2值栈,OGNL表达式,interceptor
  9. ls -l的功能是什么linux,Ls - Linux Wiki
  10. linux下配置squid 服务器,最简单使用方式
  11. 硬件基础知识-- MOS管
  12. Java Web学习总结(15)——JSP指令
  13. 更新maven卡在18不动_Android Studio maven-metadata.xml 卡着不动原因和解决方法
  14. LaTeX函数、符号及特殊字符
  15. python视频教程全集免费-Python免费视频教程
  16. 美国专利客体适格性判断标准浅析
  17. 【吴恩达】机器学习第16章异常检测以及ex8部分编程练习
  18. 世界星载SAR发展5——SIR-C(1994,美国)
  19. 视频打开后从头开始_后端软件体系结构清单:如何从头开始构建产品
  20. zabbix配置监控主机及微信报警功能

热门文章

  1. excel使用教程_办公软件excel表格制作教程
  2. 服务器延迟和时间偏移怎么设置,针对大量时间偏移配置 W32Time - Windows Server | Microsoft Docs...
  3. java final 变量 大小写_java – 为什么“final static int”可以用作开关的大小写常量但不是“final static”...
  4. vs2015 ef 连接mysql_VS2015 + EF6连接MYSQL
  5. python 实现两个excel表格数据的对比--代码
  6. 公务员因微信办公被处分!如何避免微信办公导致信息泄密?
  7. poj 1504 Adding Reversed Numbers
  8. iptables conntrack有什么用
  9. 【转】Linux下发生段错误时如何生成core文件
  10. apach2.2+php5.35 windows环境下安装问题解决