源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-07-02 09:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入理解IOS控件布局之Masonry布局框架
[b]前言:[/b] 回想起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有很大的相似之处。 [b]什么是Masonry?[/b] Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局、简洁明了、 并具有高可读性、 而且同时支持 iOS 和 Max OS X。 [b]如何使用?[/b] 1.)引入头文件 我这里是在全局引用pch文件中引用的
#import "Masonry.h"
2.)基本语法 Masonry提供的属性 [list] [*]@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;//文本基线[/*] [/list] Masonry提供了三个函数方法 [list] [*]- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //新增约束[/*] [*]- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//更新约束[/*] [*]- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有约束,只会保留最新的约束[/*] [/list] 我们根据不同的使用场景来选择使用不同的函数方法。 [b]3.)具体举例[/b]   比如一个往父控件中添加一个上下左右与父控件间距为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);
  }];
[b]特别注意:[/b] 声明约束必须在视图添加到父试图上面之后调用。 [b]4.)mas_equalTo与equalTo[/b] 上面的举例中分别使用了mas_equalTo和equalTo达到了同样的效果,我在刚开始使用Masonry的时候很容易混淆他们两个,今天特意分析一下他们的区别。mas_equalTo是一个MACRO,比较的是值,equalTo比较的是id类型。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部