Masnory笔记

概述

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的对照表如下:

36-1.png

使用

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,948评论 4 61
  • 刚搬到这栋楼的时候,邻居曾对我耳语:“某层的老太非常难搞,你注意哈!” 这个难搞,确实也让我领教了。 ...
    修炼佛心阅读 477评论 5 2
  • 是不是这一次的户外活动,点燃了大家的激情?从返程的路上,不断有人刷朋友圈,我作为一个司机,看着他们在写、在发…我真...
    廉鹏丽阅读 1,068评论 1 7
  • 太空纪元102年的某一天,正午的太阳光照射在庞大的建筑物上,投下一片黑沉沉的阴影,像是无言的墓碑。一个矮小的男人皱...
    喜悦的月阅读 776评论 5 2
  • 从前的我很自私。希望现在挽回还不算晚。你一直对我的好我竟当作理所当然。
    每天都是好心情叶小阅读 311评论 0 1

友情链接更多精彩内容