Masonry自动布局与UIScrolView适配
Masonry介绍
Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X。可以通过cocoapods将其导入。
Masonry使用
Masonry属性及其说明
//左侧 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_left; //上侧 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_top; //右侧 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_right; //下侧 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom; //首部 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading; //尾部 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing; //宽度 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_width; //高度 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_height; //横向中点 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX; //纵向中点 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY; //文本基线 //@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
其中leading与left trailing与right 在正常情况下是等价的 但是当一些布局是从右至左时(比如阿拉伯文?没有类似的经验) 则会对调 换句话说就是基本可以不理不用 用left和right就好了
- 首先介绍下UIScrollView的frame与contentsize的区别

- 重要说明
(1)UIScrollView的frame与contentsize属性的区分:UIScrollView的frame指的是这个scrollview的可视范围(可看见的区域),contentsize是其滚动范围。
(2)contentinset(不带*号的一般不是结构体就是枚举),为UIScrollView增加额外的滚动区域。(上,左,下,右)逆时针。contentinset可以使用代码或者是视图控制器进行设置,但两者有区别(注意区分)。
(3)contentsize属性只能使用代码设置。
(4)contentoffset是个CGpoint类型的结构体,用来记录ScrollView的滚动位置,即记录着“框”跑到了哪里。知道了这个属性,就知道了其位置,可以通过设置这个属性来控制这个“框”的移动。
(5)不允许直接修改某个对象内部结构体属性的成员,三个步骤(先拿到值,修改之,再把修改后的值赋回去)。
(6)增加了额外区域后,contentoffset的原点在哪里?
- 有助于理解的几个截图
模型图:

对比图:

坐标图:

Masonry与UIScrollView的自动布局:
- 首先确定UIScrollView的位置:相当于确定UIScrollView的frame
//UIScrollView滑动界面
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view);
make.left.equalTo(self.view).offset();
make.right.equalTo(self.view).offset();
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-); }];
- 适配界面1,使界面1的top,left,height,width与scrollview对齐,其中在Masonry适配中top,可以与bottom或则height确定View的竖直方位,同理Left可以与right或则width确定水平位置的方位
//1界面适配
[self.accountElectricChargeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(@);
make.left.mas_equalTo(self.scrollView.mas_left);
make.height.mas_equalTo(self.scrollView.mas_height);
make.width.mas_equalTo(self.scrollView.mas_width);
}];
- 适配界面2:因为是适配水平位置,所以top仍然与scrollView的对齐,left与第一个界面的right对齐,bottom与scrollView或则第一个界面对齐
//2界面适配
[self.accountPeakElectricView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(@);
make.left.mas_equalTo(self.accountElectricChargeView.mas_right);
make.bottom.mas_equalTo(self.scrollView);
- make.width.mas_equalTo(self.accountElectricChargeView.mas_width); }];
- 适配界面3:与界面2同理
//3界面适配
[self.accountElectricFactorView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(@);
make.left.mas_equalTo(self.accountPeakElectricView.mas_right);
make.bottom.mas_equalTo(self.scrollView);
make.width.mas_equalTo(self.accountElectricChargeView.mas_width);
}];
- 最后,使scrollview的right与第三个界面(即最后一个界面)的right对齐。
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.accountElectricFactorView.mas_right);
}];
Masonry自动布局与UIScrolView适配的更多相关文章
- Masonry自动布局:复合约束
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- Masonry自动布局
介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...
- Masonry自动布局使用
Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL.简洁明了并具有高可读性 而且同时支持 iOS 和 Max OS X. 下载 NSLayoutConstraint ...
- IOS Masonry自动布局
之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错. #import "Masonry.h" #i ...
- 【iOS】Masonry 自动布局 MASViewConstraint.m:207 错误
问题详情: Assertion failure 报错原因: make.right.equalTo([_imageView superview]).right.with.offset(-); make. ...
- Coding源码学习第四部分(Masonry介绍与使用(三))
接上篇继续进行Masonry 的学习. (12)tableViewCell 布局 #import "TableViewController.h" #import "Tes ...
- iOS开发——屏幕尺寸适配
对于屏幕尺寸适配,目前先指竖屏的方式适合方式1和2. 1.控件尺寸写死的方式,偶尔会用到屏幕的宽度和高度. UILabel *holdLabel = [[UILabel alloc]initWithF ...
- IOS控件布局之Masonry布局框架
前言: 回想起2013年做iOS开发的时候,那时候并没有采用手写布局代码的方式,而是采用xib文件来编写,如果使用纯代码方式是基于window的size(320,480)计算出一个相对位置进行布局,那 ...
- masonry使用问题
2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...
随机推荐
- delphi 八字排盘源码(post数据以后,又分析数据)
procedure TForm1.Button14Click(Sender: TObject);var ls: TStringList; lstr: string; lss: TMemorySt ...
- POJ3074 Sudoku —— Dancing Links 精确覆盖
题目链接:http://poj.org/problem?id=3074 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- 织梦CMS如何在首页调用指定的文章 idlist
在网站首页调用站内新闻是必不可少的,但是有的时候不能根据自己的需要来调用指定的文章,想要调用自己指定的文章还要做一些修改. 在网站中调用指定文章可以使用织梦默认的标签idlist,在调用的时候使用以下 ...
- for、while循环(java基础知识四)
1.循环结构概述和for语句的格式及其使用 * 什么是循环结构 循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把 ...
- 汇编环境的搭建(windows 10 + debug)
1. debug.exe 安装 win10 版本过高,不再提供 debug.exe,甚至从别处获取的 debug.exe 的也无法运行. 汇编语言学习所需的各种执行文件(debug.exe.link. ...
- 并不对劲的p4449于神之怒加强版
题目大意 给定\(t,k(t\leq2000,k\leq5*10^6)\) \(t\)组询问,每组给出\(n,m(n,m\leq5*10^6)\)求$\sum_{i=1}^n \sum_{j=1}^m ...
- BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数
BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行 ...
- nodejs URL 详解
1 我们可以使用.parse方法来将一个URL字符串转换为URL对象 例如: url.parse('http://user:pass@host.com:8080/p/a/t/h?query=strin ...
- Redis高级
Redis高级 redis数据备份与恢复 Redis SAVE 命令用于创建当前数据库的备份. redis Save 命令基本语法如下: redis 127.0.0.1:6379> SAVE 实 ...
- E20170415-ms
opaque adj 不透明的 n 不透明 adapter n 配适器