Masonry和PureLayout使用比较

首先看下Masonry和PureLayout的区别:

Masonry PureLayout
重量级,需学习成本 轻量级,语法更偏向Objective-C
添加约束 mas_makeConstraints 使用了 block 模块 没有 block
更新约束 mas_updateConstraints 保证不会导致出现两个相同约束的情况 开发者控制
删除约束 mas_remakeConstraints ,没有针对 IOS 8 使用 Active 属性 针对 IOS 8 使用 Active 属性

Massonry的用法:

    lable = [UILabel new];
    [self.view addSubview:lable];
    #prama mark  左右边距20  距离顶部20 高度40的Label
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(@(-20));
        make.top.equalTo(@20);
        make.height.equalTo(@40);
    }];
    lable.backgroundColor = [UIColor redColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = @"masonry测试Label";
    #prama mark 顶部距离label底部20 左右边距20  高度40的TextField
    UITextField* textField = [UITextField new];
    [self.view addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lable.mas_bottom).with.offset(20);
        make.left.equalTo(@20);
        make.right.equalTo(@(-20));
        make.height.equalTo(@40);
    }];
    textField.backgroundColor = [UIColor redColor];
    textField.placeholder = @"masonry测试textField";
    textField.textAlignment = NSTextAlignmentCenter;
    #prama mark 顶部距离textfield底部20  左右边距20   等宽、等高(40) 间距20的2个button
     button1 =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button1];
    button2 =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:button2];
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textField.mas_bottom).with.offset(20);
        make.left.equalTo(@20);
        make.height.equalTo(@40);
        make.width.equalTo(button2.mas_width);
        make.right.equalTo(button2.mas_left).with.offset(-20);
    }];
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(button1.mas_top);
        make.right.equalTo(@(-20));
        make.height.equalTo(button1.mas_height);
    }];
    [button1 setTitle:@"button1" forState:UIControlStateNormal];
    [button1 setBackgroundColor:[UIColor redColor]];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button1.selected = NO;
    [button1 addTarget:self action:@selector(changeSmall:) forControlEvents:UIControlEventTouchUpInside];
    
    [button2 setTitle:@"button2" forState:UIControlStateNormal];
    [button2 setBackgroundColor:[UIColor redColor]];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    #prama mark  顶部距离button底部20  相对于self.view X方向居中 宽300 高100的imageView
    imageView =[UIImageView new];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(button1.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view);
        make.height.equalTo(@100);
        make.width.equalTo(@300);
    }];

再来看看PureLayout的用法

    self.label = [[UILabel alloc] init];
    self.label.backgroundColor = [UIColor redColor];
    self.textField = [[UITextField alloc] init];
    self.textField.backgroundColor = [UIColor redColor];
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.backgroundColor = [UIColor redColor];
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.backgroundColor = [UIColor redColor];
    self.imageView = [[UIImageView alloc] init];
    self.imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.label];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.button1];
    [self.view addSubview:self.button2];
    [self.view addSubview:self.imageView];

    [self.label autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view withOffset:20.0f];
    [self.label autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-20.0f];
    [self.label autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view withOffset:20.0f];
    [self.label autoSetDimension:ALDimensionHeight toSize:40.0f];
    
    
    [self.textField autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.label withOffset:20.0f];
    [self.textField autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view withOffset:20.0f];
    [self.textField autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-20.0f];
    [self.textField autoSetDimension:ALDimensionHeight toSize:40.0f];
    
    [self.button1 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.textField withOffset:20.0f];
    [self.button1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view withOffset:20.0f];
    [self.button1 autoSetDimension:ALDimensionHeight toSize:40.0f];
    [self.button1 autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.button2];
    
    [self.button2 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.textField withOffset:20.0f];
    [self.button2 autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view withOffset:-20.0f];
    [self.button2 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.button1 withOffset:20.0f];
    [self.button2 autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.button1];
    
    [self.imageView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.button1 withOffset:20.0f];
    [self.imageView autoSetDimension:ALDimensionWidth toSize:300.0f];
    [self.imageView autoSetDimension:ALDimensionHeight toSize:100.0f];
    [self.imageView autoAlignAxisToSuperviewAxis:ALAxisVertical];

效果图如下:

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,459评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,007评论 25 709
  • 你说“女孩子,要独立。妈妈不能一直帮你做一些事情。” 他说“没关系,你在我眼里永远都是小女生,有什么事情找爸爸...
    邓枝枝阅读 1,009评论 0 0
  • 这是江南的初冬,这是被拋弃在田边的韭菜,远处的大棚里又一茬的新韭在茁壮成长。 你本应出现在人们的餐桌上供人品尝 却...
    鸿蒙一叶阅读 2,548评论 1 1
  • 这一整子还挺消极的,觉得就这么混下去吧,熬到年终奖就好了。然而产生了这种想法之后,工作提不起劲来就算了,发现连生活...
    MM菌阅读 3,165评论 0 0

友情链接更多精彩内容