概述
Masonry是一个非常轻量级的关于自动布局的库,这个相对于系统的AutoLayout的笨重给我这个习惯用纯代码来进行UI布局的人来说是一个非常不错的选择,好久没写笔记了所以特此记录下。
masnory下载地址:
OC版本: https://github.com/SnapKit/Masonry
Swift版本: https://github.com/SnapKit/SnapKit
属性
先来看看属性
@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;
这些属性与NSLayoutAttrubute的对照表如下:
使用
Masnory的绝大部分方法都放在UIViwe的类别里面:
/**
* Creates a MASConstraintMaker with the callee view.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing
*
* @param block scope within which you can build up the constraints which you wish to apply to the view.
*
* @return Array of created MASConstraints
*/
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
/**
* Creates a MASConstraintMaker with the callee view.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
* If an existing constraint exists then it will be updated instead.
*
* @param block scope within which you can build up the constraints which you wish to apply to the view.
*
* @return Array of created/updated MASConstraints
*/
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
/**
* Creates a MASConstraintMaker with the callee view.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
* All constraints previously installed for the view will be removed.
*
* @param block scope within which you can build up the constraints which you wish to apply to the view.
*
* @return Array of created/updated MASConstraints
*/
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
增加约束的方法
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
更新约束的方法
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
删除约束的方法
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;
添加约束之前必须先实例化UIView 并且把其增加到父试图上面,否则会崩溃
Masnory进阶
Masonry为NSArray添加了两个特殊方法
/*
该方法只有存有UIView的NSArray数组可以调用,NSArray里面的UIView必须有共同的spuerView
该方法作用是UIView之间的水平等宽定距约束,或者垂直等高定距约束。
先确定间距,宽度或高度不确定,但相等
axisType只有MASAxisTypeHorizontal(水平间距类型)和MASAxisTypeVertical(垂直间距类型)
fixedSpacing是UIView两两之间的间距大小
leadSpacing是第一个UIView距离superView左(或上)边界的间距大小
tailSpacing是最后一个UIView距离superView右(或下)边界的间距大小
数组里的所有UIView水平宽度相等,或者垂直高度相等
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedSpacing:(CGFloat)fixedSpacing
leadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
/*
该方法只有存有UIView的NSArray数组可以调用,NSArray里面的UIView必须有共同的spuerView
该方法作用是UIView之间的水平定宽等距约束,或者垂直定高等距约束。
先确定宽度或高度,间距不确定,但相等
axisType只有MASAxisTypeHorizontal(水平间距类型)和MASAxisTypeVertical(垂直间距类型)
fixedSpacing是UIView两两之间的间距大小
leadSpacing是第一个UIView距离superView左(或上)边界的间距大小
tailSpacing是最后一个UIView距离superView右(或下)边界的间距大小
数组里的所有UIView间距相等
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType
withFixedItemLength:(CGFloat)fixedItemLength
leadSpacing:(CGFloat)leadSpacing
tailSpacing:(CGFloat)tailSpacing;
总结
Masonry并不难 即使没接触过,不用一两个小时就能融会贯通了!主要通过多加练习就行了,对于喜欢用纯代码来布局的童鞋,Masnory是一个必不可少的库,当然现在也有很多轻量级的AutoLayout的布局,不过推荐使用Masnory