欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

深入理解IOS控件布局之Masonry布局框架

程序员文章站 2024-02-15 20:05:10
前言: 回想起2013年做ios开发的时候,那时候并没有采用手写布局代码的方式,而是采用xib文件来编写,如果使用纯代码方式是基于window的size(320,480)...

前言:

回想起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类型。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。