PureLayout的使用
来源:互联网 发布:windows 10 live mail 编辑:程序博客网 时间:2024/05/23 00:51
上次我们学到关于autolayout的代码使用,但是可能很多人写着写着就不想写了,为什么呢,代码量太多了 ,在此我附上我写的两个布局,代码如下,然后我们再去研究一下PureLayout怎么用的:
NSLayoutConstraint *constraint1=nil; NSLayoutConstraint *constraint2=nil; NSLayoutConstraint *constraint3=nil; NSLayoutConstraint *constraint4=nil; //设置登录label布局 self.toplab.translatesAutoresizingMaskIntoConstraints=NO; constraint1=[NSLayoutConstraint constraintWithItem:self.toplab attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:40]; constraint2=[NSLayoutConstraint constraintWithItem:self.toplab attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0]; NSArray *topconstarray=@[constraint1,constraint2]; [self.view addConstraints:topconstarray]; //设置帐号输入框布局 self.accountField.translatesAutoresizingMaskIntoConstraints=NO; constraint1=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.toplab attribute:NSLayoutAttributeTop multiplier:1.0f constant:20]; constraint2=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10]; constraint3=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:10]; constraint4=[NSLayoutConstraint constraintWithItem:self.accountField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30]; NSArray *accountconstarray=@[constraint1,constraint2,constraint3,constraint4]; [self.view addConstraints:accountconstarray]; //用autolayout代码太多了,受不了了,下面的就用第三方purelayout了
现在我们来学习如何使用PureLayout,其实PureLayout前身是(UIView+AutoLayout),作者是同一人,为什么推荐使用这个第三方呢?因为它封装都比较形象,并且接近底层,对理解AutoLayout的原理和掌握内涵更加的有益。
接下来我们来看一下如何使用吧:
首先我们下载PureLayout:http://www.oschina.net/p/purelayout
在头文件导入PureLayout.h文件
然后我们就来研究一下每一个方法的意思吧
首先我们在 [super viewDidLoad]之后创建一个UIView,UILabel 和UIButton来进行说明:
UIView *myview=[[UIView alloc]init]; UILabel *label=[[UILabel alloc]init]; UIButton *btn=[[UIButton alloc]init]; myview.translatesAutoresizingMaskIntoConstraints=NO; label.translatesAutoresizingMaskIntoConstraints=NO; btn.translatesAutoresizingMaskIntoConstraints=NO; [self.view addSubview:myview]; [self.view addSubview:label]; [self.view addSubview:btn];
接下来我们就来看看purelayout封装了哪些好用的布局方法:
// myview左边距离父视图左边 10 点. [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10.f]; // myview1顶边距离父视图顶部 10 点. [myview autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.f]; // myview的左边位于btn的右边 10 点的地方. [myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:btn withOffset:10.f]; // myview的右边位于btn左边 -10 点的地方, 从右往左、从下往上布局时数值都是负的。 [myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:btn withOffset:-10.f]; // 根据label的固有内容尺寸设置它的尺寸 [label autoSetDimensionsToSize:CGSizeMake(10.f, 10.f)]; // myview、label、btn水平对齐。(意思就是说只需要设置其中一个的垂直约束(y)即可)(这个可以设置多个view水平对齐) [@[myview, label, btn] autoAlignViewsToAxis:ALAxisHorizontal]; //myview,label水平对齐(这个是针对两个view的水平对齐) [myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label]; // myview顶部距离label的底部 10 点. [myview autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:label withOffset:10.f]; // myview左边紧贴父视图左边 [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading]; // myview的宽度等于父视图的宽度 [myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.view]; // myview的宽度设置为50 [myview autoSetDimension:ALDimensionWidth toSize:30]; // myview与label左对齐 [myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:label]; // myview与label水平对齐 [myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label]; //myview右边雨label左边的距离大于等于20 [myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:label withOffset:20.f relation:NSLayoutRelationGreaterThanOrEqual]; //myview和btn同宽(同高也一样) [@[myview,btn] autoMatchViewsDimension:ALDimensionWidth];//这种可以设置多个view同宽 [myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:btn];//这种只能设置两个view同宽
当然这里只是列举了一些常用的方法,其实只要懂得autolayout的基本原理,学会了autolayout,PureLayout中提供的方法都能推算出什么意思。怎么样,用PureLayout会比autolayout简化很多代码量吧!
注意:这里我主要讲一下NSLayoutAttributeLeft ,NSLayoutAttributeRight,和NSLayoutAttributeLeading,NSLayoutAttributeTrailing之间的区别。
其实在中国, NSLayoutAttributeLeft 和 NSLayoutAttributeLeading 是一个效果的,因为我们布局习惯从左到右。但在有些国家地区,NSLayoutAttributeRight和NSLayoutAttributeLeading 是一个效果,布局习惯从右往左。这点我们大可放心,建议大家还是使用NSLayoutAttributeLeading,NSLayoutAttributeTrailing来表示左和右。
- PureLayout的使用
- PureLayout的基本使用(一)
- iOS PureLayout的基本使用(一)
- iOS PureLayout的基本使用(二)
- PureLayout的部分用法
- PureLayout
- pureLayout的九宫格布局
- PureLayout,使用纯代码写AutoLayout
- PureLayout和Masonry的比较以及对Masonry的扩展
- 任务相关界面的布局(Purelayout),scrollView上建立约束
- PureLayout 加 动画
- Purelayout小结1
- Xcode 6自动布局第三方类PureLayout
- iOS】利用PureLayout实现:比例自动布局(AutoLayout)
- 一点笔记 about layout 兼purelayout + 需要自动布局对scrollView影响
- GUID使用的使用
- ${}的使用
- 的使用
- java - sap 概念
- C#对字符串的简单加密解密过程C#的SQL数据库登陆密码的加密解密加密还原直接复制代码使用即可
- C# ADO基础(使用using操作数据库)
- Oracle SQL性能优化
- Unity 利用itextSharp导出pdf文档
- PureLayout的使用
- Evaluate a Cubic Bézier on GPU
- 【SCOI2011 Day1】糖果
- 2015 Multi-University Training Contest 3--1011 Work
- swift基础语法
- Java log4j日志记录工具配置说明
- 数据库常见面试题
- 控件——radioButton
- Android播放HLS协议的流媒体