框架名称 | git地址 | 最后更新时间 | 支持语言 | 评价星级 | 备注 |
Masonry | https://github.com/SnapKit/Masonry | 最后更新5天前(4.15) | Objective-C/Swift(snapkit) | 11,276 | Supports iOS and OSX Auto Layout |
PureLayout | https://github.com/PureLayout/PureLayout | 最后更新21 Feb | Objective-C/Swift | 5,225 | iOS & OS X Auto Layout |
MMPlaceHolder | https://github.com/adad184/MMPlaceHolder | 最后更新是2015年10.20 | Objective-C | 789 | 这个只是显示高宽的插件 |
UIView-AutoLayout | https://github.com/smileyborg/UIView-AutoLayout | 最后更新23 Jun 2014 | Objective-C | 1,443 | Deprecated in favor of PureLayout |
Cartography for swift | https://github.com/robb/Cartography | 最后更新9天前(4.11) | Swift | 4,160 | 仅支持swift |
基于以上几点,可以从masonry和PureLayout中选出
(用原生的 NSLayoutConstraint 手写代码是反人类的,代码量高,开发成本高)
|
Masonry
|
PureLayout
|
---|---|---|
学习成本 | 轻量级、简单易学、傻瓜式 | 对autolayout封装的比较浅,代码看起来跟oc风格比较像,首先要会原生的autolayout,不然学习成本特别高 |
使用便捷度 | api比较少,基本上就3个,很容易记住,优点在于把数学的函数,翻译成可读性极佳的代码,像读句子一样,人好读了,写起来思维流畅,就不容易出错 | api太多,需要记住每个符号的含义,即使有代码自动提示,使用起来还是很不方便,易出错。 |
开发成本 | 书写简介,代码量少 | 书写复杂,与原生比较,没有太大的优化,冗长,啰嗦 |
支持语言 | 需要下载snapkit | 同时支持 |
更新约束 | mas_updateConstraints 保证不会导致出现两个相同约束的情况 | 开发者控制 |
删除约束 | mas_remakeConstraints ,没有针对 IOS 8 使用 Active 属性 | 针对 IOS 8 使用 Active 属性 |
添加约束 | mas_makeConstraints 使用了 block 模块,作为设置声明的清单,层次明确 | 没有block,需要手动划分约束层次 |
口碑/支持率 | 11,276 | 5,225 |
示例:
原生:
UIView *superview = self.view; UIView *view1 = [[UIView alloc] init]; view1.translatesAutoresizingMaskIntoConstraints = NO; view1.backgroundColor = [UIColor greenColor]; [superview addSubview:view1]; UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]]; masonry:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding); }]; PureLayout:
[view1 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10.f];
[view1 autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.f];
[view1 autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10.f];
[view1 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10.f];