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

苹果自带的自动布局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);

}];

iOS 自动布局第三方框架Masonry的使用_第1张图片

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);

}];


iOS 自动布局第三方框架Masonry的使用_第2张图片

你可能感兴趣的:(iOS 自动布局第三方框架Masonry的使用)