貌似又好久没有上博客了,近期在做IOS开发,原来开发过一个项目用的都是xib、或故事版,然而在开发复杂的页面真的显得心有余而力不足,然后在一些页面采用了代码布局,最坑爹的一开始使用的是frame布局,又错点把自己坑死,后来使用了约束布局,纯原生的那种。又错点奔溃了。近期闲下来了,网上搜索一番,发现Masonry 还是比较火的,故学习了一番,果然很流弊,虽然API没有多少,苦恼自己从学了IT记性越来越差劲了,但是每次找资料,都是长篇大论的入门级教程,顾现在把所有的API都罗列出来,并且简单备注,主要是帮助自己和了解过Masonry 的朋友,快速查找和使用,当然英语大牛除外,因为源码都有注释,直接看就行。哈哈
//无非就是增删改 ,不错就是这三个block
//这个方法只会添加新的约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.top).offset(padding);
}];
//这个方法会将以前的约束全部删除,添加新的约束
[view mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
//这个方法将会覆盖以前的某些特定的约束
[view mas_updateConstraints:^(MASConstraintMaker *make) {
}];
1、Basic Attribute: 基本属性,支持到iOS6,一般使用得比较多
@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;
//尾部其中leading与left trailing与right 在正常情况下是等价的 但是当一些布局是从右至左时(比如阿拉伯文?没有类似的经验) 则会对调 换句话说就是基本可以不理不用 用left和right就好了
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
//横向基线 比如
make.centerX.equalTo(self.view); 就是self.view的横向中间了。
@property (nonatomic, strong, readonly) MASConstraint *centerX;
//纵向基线
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
//margin系列
@property (nonatomic,strong, readonly)MASViewAttribute *leftMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *rightMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *topMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *bottomMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *leadingMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *trailingMargin;
@property (nonatomic,strong, readonly)MASViewAttribute *centerXWithinMargins;
@property (nonatomic,strong, readonly)MASViewAttribute *centerYWithinMargins;
3、Convenient Attribute: 便捷属性,为了使用方便而特意新增的属性。Autolayout本身没有对应的相关属性
//Convenient Attribute
make.edges.insets(edge);
//Basic Attribute
make.left.right.top.bottom.insets(edge);
Relationship
约束表示的是2个item之间的关系,在Autolayout中一共定义了3种关系:=, >=, <=,对应到Masonry中:
- (MASConstraint * (^)(id attr))equalTo;
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;
作者:小笨狼
链接:http://www.jianshu.com/p/1841e6c69611
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。