Masonry使用

来源:互联网 发布:淘宝流量接单任务平台 编辑:程序博客网 时间:2024/05/17 08:44

Masonry

    1.1 Masonry基本概念

    Masonry一个轻量级的布局框架,采用更"优雅"的语法封装自动布局,不需要使用XIBStoryboard并具有高可读性 而且同时支持 iOS Max OS X

    Masonry尤其适合习惯纯代码开发的开发者,在iPhone6发布后引发的适配潮中 Masonry一定可以助你一臂之力

    》框架下载地址

    https://github.com/Masonry/Masonry

    》常用属性含义(View+MASShorthandAdditions.h

    /*

     View+MASShorthandAdditions中属性与NSLayoutAttrubute的对照表如下

     Masonry NSAutoLayout说明

     left       NSLayoutAttributeLeft 左侧

     top        NSLayoutAttributeTop 上侧

     right      NSLayoutAttributeRight 右侧

     bottom     NSLayoutAttributeBottom 下侧

     leading NSLayoutAttributeLeading首部

     trailing NSLayoutAttributeTrailing尾部

     width      NSLayoutAttributeWidth     

     height     NSLayoutAttributeHeight    

     centerX NSLayoutAttributeCenterX横向中点

     centerY NSLayoutAttributeCenterY纵向中点

     baseline NSLayoutAttributeBaseline文本基线

     

     注意:iOS8中新增的属性Masonry暂时还不支持(提示:现阶段我们开发的APP要支持ios6,ios7所以可以忽略)

     */

    1.2Masonry基本使用

    》利用代码实现在控制器上添加一个控件,宽高等于200并且永远在屏幕中间

    》利用VFL实现如上

    》用Masonry实现如上

    1.3Masonry常用方法

    》三个添加约束方法区别

/*

mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束否则会报错

mas_updateConstraints针对上面的情况 会更新在block中出现的约束不会导致出现两个相同约束的情况

mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

三种函数善加利用就可以应对各种情况了

 */

    》两个赋值方法区别(equalTo mas_equalTo

/*

 #define equalTo(...)                     mas_equalTo(__VA_ARGS__)

 #define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))

 

 mas_equalTo对其参数进行了一个自动装箱操作,除了支持NSNumber数值类型之外还支持CGPoint CGSize UIEdgeInsets

 */

    1.4Masonry练习

    》在控制器上添加一个控件,距离控制器View上下左右各20

      方式一逐个设置 make.top.equalTo

      方式二连续设置 make.top.left.bottom.and.right.equalTo

/*

 注意:这里的andwith其实这两个函数什么事情都没做

 andwith方法直接 return self;

 */

      方式三一次性设置 make.edges.equalTo


    》快速入门文章

http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/


iOS8新特性介绍:

https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html#//apple_ref/doc/uid/TP40014205-SW1


0 0
原创粉丝点击