美文网首页
iOS 自动布局第三方框架Masonry的使用

iOS 自动布局第三方框架Masonry的使用

作者: 顺其自然2017 | 来源:发表于2017-08-27 23:24 被阅读0次

苹果自带的自动布局AutoLayout实现方式令人望而生畏,但还是包含着不错的思想,有人封装了第三方Masonry,试了试,感觉不错。

Masonry  点击获取Masonry第三方SDK

下载完Masonry后,导入Masonry框架,有两种方式 CocoaPod或者直接拖拽添加到资源项目文件中,删除Masonry文件夹内的plist文件,防止冲突。

Masonry对AutoLayout做了比较精简的封装,是的对视图的约束条件更加的简洁明确,布局目标一目了然,非常不错。

三个核心的方法如下

添加约束

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {

}

更新约束

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block {

}

重写约束

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block {

}

//以下是常用的属性

@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; //文本基线

//以下是常用的关系比较函数( 大于等于,小于等于,等于)

- (MASConstraint * (^)(id))equalTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationEqual);

};

}

- (MASConstraint * (^)(id))mas_equalTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationEqual);

};

}

- (MASConstraint * (^)(id))greaterThanOrEqualTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);

};

}

- (MASConstraint * (^)(id))mas_greaterThanOrEqualTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);

};

}

- (MASConstraint * (^)(id))lessThanOrEqualTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);

};

}

- (MASConstraint * (^)(id))mas_lessThanOrEqualTo {

return ^id(id attribute) {

return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);

};

}

其实还有很多东西,想要全面了解,还是看SDK源代码比较好

下面是简单的使用示例,想要更多的了解,亲自试验是王道。

UIView *v1=[[UIView alloc]init];

v1.backgroundColor=[UIColor redColor];

[self.view addSubview:v1];

__weak __typeof(&*self)weakSelf = self;//防止循环引用

//设置v1视图红色并且长宽各100,位于父视图色self.view中心

[v1 mas_makeConstraints:^(MASConstraintMaker* make){

make.size.mas_equalTo(CGSizeMake(100, 100));

make.center.equalTo(weakSelf.view);

}];

UIView *v1=[[UIView alloc]init];

v1.backgroundColor=[UIColor redColor];

[self.view addSubview:v1];

UIView *v2=[[UIView alloc]init];

v2.backgroundColor=[UIColor greenColor];

[self.view addSubview:v2];

__weak __typeof(&*self)weakSelf = self;

//设置v1视图红色并且长宽各100,位于父视图色self.view中心

[v1 mas_makeConstraints:^(MASConstraintMaker* make){

make.size.mas_equalTo(CGSizeMake(100, 100));

make.center.equalTo(weakSelf.view);

}];

//设置视图v2绿色并且距离父视图上左右各100,高度不超过100

[v2 mas_makeConstraints:^(MASConstraintMaker* make){

make.top.mas_equalTo(weakSelf.view).offset(100);

make.left.mas_equalTo(weakSelf.view).offset(100);

make.right.mas_equalTo(weakSelf.view).offset(-100);

make.height.mas_equalTo(@100);

}];

相关文章

  • Third Party

    A:推荐使用 B:修改使用 C:参考使用 自动布局Masonry(A)iOS自动布局框架-Masonry详解SDA...

  • 适配

    Masonry iOS自动布局框架-Masonry详解

  • iOS常用第三方框架

    iOS常用第三方框架 自动布局框架Masonry // Objective-C https://github....

  • Masonry源码分析

    iOS 源代码分析 --- Masonry Masonry 是 Objective-C 中用于自动布局的第三方框架...

  • Masonry源码分析与链式编程

    在ios开发中,Masonry是最常用的第三方开发布局框架。Masonry是基于自动布局技术实现的,所以说Maso...

  • Masonry源码解析

    Masonry简介 Masonry是用于自动布局的第三方框架,对苹果的自动布局框架进行了一层封装,其接口比起官方的...

  • Masonry分析

    iOS 源代码分析----Masonry Masonry是OC自动布局的框架,简化了AutoLayout的写法。 ...

  • 使用Masonry代码进行屏幕适配的详细介绍

    Masonry自动布局使用 Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL...

  • swift中Masonry的布局

    iOS SnapKit自动布局使用详解(Swift版Masonry) 对于自动布局: 我们在 StoryBoard...

  • iOS 的布局

    iOS布局可以xib文件布局,也可以代码布局,代码布局一般使用第三方框架masonry可以用pods导入mason...

网友评论

      本文标题:iOS 自动布局第三方框架Masonry的使用

      本文链接:https://www.haomeiwen.com/subject/kvrbdxtx.html