Masonry

作者: star_night | 来源:发表于2017-10-18 20:12 被阅读0次

    一、调用Masonry库的准备

    使用pch文件添加全局宏定义(Pre-Compiled Header)
    pch文件即 扩展名为.pch的预编译文件。是将工程中较稳定的不会经常修改的代码预先编译好,放在一个公共的文件(.pch)里。

    0>将Masonry文件拖入项目中
    1>新建pch文件



    2>选中项目-->TARGETS-->Build Setting(选中All)-->搜索prefi



    3>双击并将pch文件拖入框中
    4>设置为Yes

    5>填入代码

    #ifndef PrefixHeader_pch
    #define PrefixHeader_pch
    
    //define this constant if you want to use Masonry without the 'mas_' prefix
    #define MAS_SHORTHAND
    //define this constant if you want to enable auto-boxing for default syntax
    #define MAS_SHORTHAND_GLOBALS
    #import "Masonry.h"
    
    // Include any system framework and library headers here that should be included in all compilation units.
    // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
    
    #endif /* PrefixHeader_pch */
    

    二 、使用方法

    1.Masonry属性

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

    下面是关于这些属性的介绍



    2.约束的类型

     1.尺寸:width、height、size
     2.边界:left、leading、right、trailing、top、bottom
     3.中心点:center、centerX、centerY
     4.边界:edges
     5.偏移量:offset、inset、insets、sizeOffset、centerOffset
     6.优先级:priority()约束优先级(0~1000)、priorityLow()\*500*\、priorityMedium()\*500*\、priorityHigh()\*750*\
     7.比例:multipliedBy乘因数、 dividedBy除因数
    

    3.两个区别

    1>mas_equalToequalTo:

    默认情况下mas_equalTo有自动包装功能,比如自动将20包装为@20equalTo没有自动包装功能。

    如果添加了下面的宏,那么mas_equalToequalTo就没有区别:
    #define MAS_SHORTHAND_GLOBALS

    2>mas_heightheight:

    默认情况下widthmake对象的一个属性,用来添加宽度约束用的,表示对宽度进行约束。mas_width是一个属性值,用来当做equalTo的参数,表示某个控件的宽度属性。

    如果添加了下面的宏,mas_width也可以写成width:
    #define MAS_SHORTHAND

    4.基本使用

    mas_makeConstraints://添加约束
    mas_updateConstraints://更新约束、亦可添加新约束
    mas_remakeConstraints://重置之前的约束
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superview).with.insets(padding);
    }];
    

    5.注意点

    使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraints属性为NO;
    防止block中的循环引用,使用弱引用(这是错误观点),在这里block是局部的引用,block内部引用self不会造成循环引用的
    __weak typeof (self) weakSelf = self;(没必要的写法)

    相关文章

      网友评论

          本文标题:Masonry

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