美文网首页
Masnory笔记

Masnory笔记

作者: 小强七号 | 来源:发表于2016-08-18 10:51 被阅读313次

概述

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

相关文章

  • Masnory笔记

    概述 Masonry是一个非常轻量级的关于自动布局的库,这个相对于系统的AutoLayout的笨重给我这个习惯用纯...

  • masnory方法笔记

    在使用masnory适配的时候,有些方法记不住,特此笔记记录。 1、居中设置 2、边界设置 3、倍数设置 4、大小...

  • Masnory 使用笔记

    使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraint...

  • Masnory动画小记

    Masnory 不需要weakSelf masnory中的block使用原理和UIView 动画中的block 以...

  • Masnory

    居中显示 make.center.equalTo(self.view);make.size.mas_equalTo...

  • masnory 动画

    简单的动画就是把更改位置这件事放在一个时间里,所以重新修改约束,更新约束,设置全局约束更改值。

  • iOS 知识点-Masnory约束UIScrollerView的

    Masnory约束UIScrollerView 直接用Masonry对scrollView进行约束,scrollV...

  • Masonry 使用注意

    1,如何打印 frame 2,UIScrollView 如何 使用 masnory 3,利用 masonry 使...

  • ScrollView自适应Masnory

    ScrollView自适应Masnory 可以展示, 但是不能接收点击

  • masnory设置圆角

    对masnory不是很熟悉.一直觉得用它无法得到frame,其实不然.用过layoutIfNeeded 这个方法后...

网友评论

      本文标题:Masnory笔记

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