iOS界面约束第三方框架对比

框架名称 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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 MJPhotoBrowser 框架实现图片浏览器的步骤如下: 1. 首先需要使用 CocoaPods 将 MJPhotoBrowser 框架导入到项目中。 2. 导入框架头文件:#import "MJPhotoBrowser.h" 3. 在需要显示图片浏览器的地方,创建一个数组用来存放图片的 URL 或 UIImage 对象。 4. 遍历图片数组,将每张图片转换成 MJPhoto 对象,并将其添加到 MJPhotoBrowser 控制器的 photoArray 属性中。 5. 创建 MJPhotoBrowser 控制器对象,并设置其 currentPhotoIndex 属性为当前图片的下标。 6. 调用 presentViewController:animated:completion: 方法,将 MJPhotoBrowser 控制器推出来展示图片浏览器。 以下是示例代码: ```objc // 创建图片数组 NSMutableArray *photos = [NSMutableArray array]; for (int i = 0; i < self.imageArray.count; i++) { // 创建 MJPhoto 对象 MJPhoto *photo = [[MJPhoto alloc] init]; // 设置图片的 URL 或 UIImage 对象 photo.url = [NSURL URLWithString:self.imageArray[i]]; // 设置图片所对应的原始 UIImageView photo.srcImageView = self.imageViewArray[i]; // 添加到图片数组中 [photos addObject:photo]; } // 创建 MJPhotoBrowser 控制器对象 MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init]; // 设置图片数组 browser.photos = photos; // 设置当前显示的图片下标 browser.currentPhotoIndex = index; // 显示图片浏览器 [self presentViewController:browser animated:YES completion:nil]; ``` 其中,self.imageArray 和 self.imageViewArray 分别为存放图片 URL 或 UIImageView 对象的数组,index 为当前需要显示的图片下标。在上述代码中,我们将图片 URL 和对应的 UIImageView 对象一起存放到 MJPhoto 对象中,这样在图片浏览器中浏览时,就可以自动放大到对应的 UIImageView 的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值