IOS 控件布局之 Masonry 布局框架

2,078 阅读3分钟

前言:

  回想起2013年做iOS开发的时候,那时候并没有采用手写布局代码的方式,而是采用xib文件来编写,如果使用纯代码方式是基于window的size(320,480)计算出一个相对位置进行布局,那个时候windows的size是固定不变的,随着iphone5的发布,windows的size(320,568)也发生了变化,而采用autoresizingMask的方式进行适配,到后来iphone 6之后windows size的宽度也随之变化,开始抛弃autoresizingMask改用autolayout了,使用autolayout进行适配我也是最近重新做iOS开发才接触的,公司使用Masonry框架进行布局适配。所以学习使用这个布局框架对我来说至关重要,它大大提高了开发效率而且最近使用起来很多语法和Android有很大的相似之处。

什么是Masonry?

  Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局、简洁明了、 并具有高可读性、 而且同时支持 iOS 和 Max OS X。

如何使用?

1.)引入头文件

我这里是在全局引用pch文件中引用的

#import "Masonry.h"

2.)基本语法

Masonry提供的属性

  • @property (nonatomic, strong, readonly) MASConstraint *left;//左侧
  • @property (nonatomic, strong, readonly) MASConstraint *top;//上侧
  • @property (nonatomic, strong, readonly) MASConstraint *right;//右侧
  • @property (nonatomic, strong, readonly) MASConstraint *bottom;//下侧
  • @property (nonatomic, strong, readonly) MASConstraint *leading;//首部
  • @property (nonatomic, strong, readonly) MASConstraint *trailing;//尾部
  • @property (nonatomic, strong, readonly) MASConstraint *width;//宽
  • @property (nonatomic, strong, readonly) MASConstraint *height;//高
  • @property (nonatomic, strong, readonly) MASConstraint *centerX;//横向居中
  • @property (nonatomic, strong, readonly) MASConstraint *centerY;//纵向居中
  • @property (nonatomic, strong, readonly) MASConstraint *baseline;//文本基线

Masonry提供了三个函数方法

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增约束
  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新约束
  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有约束,只会保留最新的约束

我们根据不同的使用场景来选择使用不同的函数方法。

3.)具体举例

  比如一个往父控件中添加一个上下左右与父控件间距为50的子视图

添加约束

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.mas_equalTo(50);
        make.bottom.mas_equalTo(-50);
    }];

等价于

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.top.equalTo(self.view.mas_top).offset(50);
        make.bottom.equalTo(self.view.mas_bottom).offset(-50);
    }];

也可以简化为下面这种

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(50, 50, 50, 50));
    }];

又等价于

    UIView *tempView=[[UIView alloc]init];
    tempView.backgroundColor=[UIColor greenColor];
    [self.view addSubview:tempView];
    
    [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(50, 50, 50, 50));
    }];

更新约束

    [tempView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.top.mas_equalTo(100);
        make.bottom.mas_equalTo(-100);
    }];

清除之前的约束保留最新的

    [tempView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(100);
        make.right.mas_equalTo(-100);
        make.top.mas_equalTo(100);
        make.bottom.mas_equalTo(-100);
    }];

特别注意:

   声明约束必须在视图添加到父试图上面之后调用。

4.)mas_equalTo与equalTo

 上面的举例中分别使用了mas_equalTo和equalTo达到了同样的效果,我在刚开始使用Masonry的时候很容易混淆他们两个,今天特意分析一下他们的区别。mas_equalTo是一个MACRO,比较的是值,equalTo比较的是id类型。

总结:

  简单记录一下Masonry布局框架的使用。

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!