PureLayout 是 iOS & OS X Auto Layout 的终极 API——非常简单,又非常强大。PureLayout 通过一个全面的Auto Layout API 扩展了 UIView/NSView, NSArray 和 NSLayoutConstraint,仿照苹果自身的框架, 构建了一个全面的自动布局 API, 这样你再也不用为适配而苦恼啦!!!

添加PureLayout到你的工程里面

  • 用CocoaPods安装(podilfe中加pod 'PureLayout')/GitHub下载PureLayout, 手动添加到你的项目中
  • 导入头文件#import <PureLayout/PureLayout.h> /  #import "PureLayout.h"

我们就这个布局来简单说一下

首先新建几个 View

1
2
3
4
5
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *yellowView;
@property (nonatomic, strong) UIView *greenView;
@property (nonatomic, assign) BOOL didSetupConstraints; // 判断是否存在约束条件

然后在加载试图

1
2
3
4
5
6
7
8
9
10
11
- (void)loadView
{
self.view = [UIView new];
self.view.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0]; [self.view addSubview:self.blueView];
[self.view addSubview:self.redView];
[self.view addSubview:self.yellowView];
[self.view addSubview:self.greenView];
[self.view setNeedsUpdateConstraints]; // 设置新的约束天剑
}

如果没有试图,那么就重新创建一个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- (UIView *)blueView
{
if (!_blueView) {
_blueView = [UIView newAutoLayoutView];
_blueView.backgroundColor = [UIColor blueColor];
}
return _blueView;
} - (UIView *)redView
{
if (!_redView) {
_redView = [UIView newAutoLayoutView];
_redView.backgroundColor = [UIColor redColor];
}
return _redView;
} - (UIView *)yellowView
{
if (!_yellowView) {
_yellowView = [UIView newAutoLayoutView];
_yellowView.backgroundColor = [UIColor yellowColor];
}
return _yellowView;
} - (UIView *)greenView
{
if (!_greenView) {
_greenView = [UIView newAutoLayoutView];
_greenView.backgroundColor = [UIColor greenColor];
}
return _greenView;
}

然后再添加试图的约束条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
- (void)updateViewConstraints   //更新约束条件
{
// 如果没有自动约束条件
if (!self.didSetupConstraints) {
// Blue view is centered on screen,(Centers the view in its superview.) with size {50 pt, 50 pt} //设置蓝色的 view 在父试图的中心,
[self.blueView autoCenterInSuperview];
//设置蓝色的 view 的宽和高(50.0 50.0)
[self.blueView autoSetDimensionsToSize:CGSizeMake(50.0, 50.0)]; // Red view is positioned at the bottom right corner of the blue view, with the same width, and a height of 40 pt
// 设置红色 view 的顶部是蓝色 view 的底部
[self.redView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.blueView];
//设置红色 view 的左边是蓝色 view 的右边
[self.redView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.blueView];
//设置红色 view 的宽度就是蓝色 view 的宽度
[self.redView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.blueView];
//设置红色 view 的高度为40.0
[self.redView autoSetDimension:ALDimensionHeight toSize:40.0]; // Yellow view is positioned 10 pt below the red view, extending across the screen with 20 pt insets from the edges,
// and with a fixed height of 25 pt
//设置黄色 veiw 的顶部距离红色 view 距离为10.0
[self.yellowView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.redView withOffset:10.0];
//设置黄色 view 的高度为25.0
[self.yellowView autoSetDimension:ALDimensionHeight toSize:25.0];
//设置黄色 view 距离父试图左边的距离为20.0
[self.yellowView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20.0];
//设置黄色 view 距离父试图右边的距离为20.0
[self.yellowView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20.0]; // Green view is positioned 10 pt below the yellow view, aligned to the vertical axis of its superview,
// with its height twice the height of the yellow view and its width fixed to 150 pt
//设置绿色 view 的顶部距离黄色 view 的底部距离为10.0
[self.greenView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.yellowView withOffset:10.0];
//设置绿色 view 相对父试图竖向居中
[self.greenView autoAlignAxisToSuperviewAxis:ALAxisVertical];
//设置绿色的 view 的高度是黄色 view 高度的2.0倍
[self.greenView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.yellowView withMultiplier:2.0];
//设置绿色 view 的宽度为150.0
[self.greenView autoSetDimension:ALDimensionWidth toSize:150.0]; //设置已经添加过约束了
self.didSetupConstraints = YES;
} [super updateViewConstraints];
}

github 地址:https://github.com/smileyborg/PureLayout#

PureLayout的更多相关文章

  1. PureLayout和Masonry比较

    一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...

  2. iOS PureLayout使用

    PureLayout是iOS Auto Layout的终端API,强大而简单.由UIView.NSArray和NSLayoutConstraint类别组成.PureLayout为大多数Auto Lay ...

  3. PureLayout,使用纯代码写AutoLayout

    为iOS和OS X的自动布局最终的API -- 令人印象深刻的简单,非常强大. PureLayout延伸的UIView /NSView , NSArray,和NSLayoutConstraint与之后 ...

  4. PureLayout(轻量级自动布局)

    直接整理用法 1.设置高度宽度 [view1 autoSetDimension:ALDimensionHeight toSize:70.0]; [view1 autoSetDimension:ALDi ...

  5. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  6. 【转】GitHub 排名前 100 的安卓、iOS项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

  7. GitHub iOS-Top 100 简介

    GitHub排名前100的iOS第三方汇总简介,方便开发者选择适合的第三方框架. 项目名称 项目信息 1. AFNetworking 作者是 NSHipster 的博主, iOS 开发界的大神级人物, ...

  8. iOS 开源项目

    在 Github 上 Star 太多了,有时候很难找到自己想要的开源库,所以在此记录下来.便于自己开发使用,也顺便分享给大家. 动画 awesome-ios-animation收集了iOS平台下比较主 ...

  9. Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)

    下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...

随机推荐

  1. Mac OS启动服务优化高级篇(launchd tuning)

    Mac下的启动服务主要有三个地方可配置:1,系统偏好设置->帐户->登陆项2,/System/Library/StartupItems 和 /Library/StartupItems/3, ...

  2. 支付宝AR实景红包上线不久即遭破解,官方已提高技术门槛

    临近春节,阿里巴巴和腾讯的红包大战可谓下足功夫,上周支付宝推出了AR实景红包,该玩法基于"LBS+AR+红包"的方式,类似与今年火爆全球的AR手游Pekomon Go ,只不过这次 ...

  3. 启动Mysql报错:Another MySQL daemon already running with the same unix socket.

    启动Mysql报错: Another MySQL daemon already running with the same unix socket. 删除如下文件即可解决 /var/lib/mysql ...

  4. 洛谷-乘积最大-NOIP2000提高组复赛

    题目描述 Description 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你 ...

  5. CentOS6.5 安装mysql5.6.30

    1.下载解压由于系统会自带mysql5.1版本的数据库,需要卸载.[root@localhost src]# yum remove -y mysql-libs[root@localhost src]# ...

  6. js调用函数时传入的参数个数与函数定义时的参数个数不符时的操作

    在js中函数没有重载的概念,如果声明了多个重名的函数,不管函数的形参个数是否一样,只有最有一个有效,其他的函数声明都是无效的.比如说声明了两个函数fn(),第一次声明时没有形参,第二次声明时形参有两个 ...

  7. windows 7 & protobuf 3.0 & python 3.5

    置顶: 在Python中使用protocol buffers参考指南 http://blog.csdn.net/losophy/article/details/17006573 其实看这篇文章就可以把 ...

  8. 第13天 JSTL标签、MVC设计模式、BeanUtils工具类

    第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1.    JSTL的核心标签库使用必须会使用    1 1.1.    c:if标签    1 1.2.    c:choos ...

  9. 草料生成app自动下载的二维码

    草料官网http://cli.im/app 填写iOS和安卓的appid就好了

  10. 《JS权威指南学习总结--6.4检测属性》

    内容要点: js对象可以看做属性的集合,我们经常会检测集合中成员的所属关系-----判断某个属性是否存在于某个对象中,可以通过in运算符,hasOwnPreperty()和propertyIsEnum ...