Masonry使用技巧

作者: coderhlt | 来源:发表于2018-03-15 16:15 被阅读3次

一、相同View水平、竖直方向上排布

//- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;

//- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
使用例子:

  for (int i=0; i<10; i++) {
       UIView *view=[[UIView alloc]init];
       [self.view  addSubview:view];
       view.backgroundColor=[UIColor redColor];
       [array addObject:view];
   
   }
   //等固定大小垂直方向上排布
   [array mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:30 leadSpacing:10 tailSpacing:10];
   [array mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.view).offset(20);
       make.width.equalTo(@40);
   }];
     //等固定大小水平方向上排布
   [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:10 tailSpacing:10];
   [array mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.view).offset(20);
       make.height.equalTo(@40);
   }];

   //等间距水平方向上排布
   [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
   [array mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(self.view).offset(20);
           make.height.equalTo(@40);
       }];
   
   //等间距垂直方向上排布
   [array mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
   [array mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.view).offset(20);
       make.width.equalTo(@40);
   }];

二、lable显示多行需要得到高度

UILabel *lable=[[UILabel alloc]init];
   [self.view addSubview:lable];
   lable.text=@"1234567890123456789qqqqddd问问呜呜呜fffffffffggggggrrr0";
   lable.numberOfLines=0;
   [lable mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.top.equalTo(self.view).offset(100);
       make.width.equalTo(@100);
    }];
   lable.preferredMaxLayoutWidth=100;//如果你不需要拿到系统计算的高度值,你就没必要设置;如果你希望拿到这个值,建议你最好设置(因为在开发中发现,如果不设置的话拿到的值就不准确)
   
   //强制更新一下,如果不更新约束并不会马上转成frame,打印的值为0
   [lable layoutIfNeeded];
   
   //得到期望值
   NSLog(@"%f",lable.frame.size.height);

三、UIScrollview遇到Masonry。

UIScrollView是个非常特殊的view UIScrollView与其subview之间相对位置的约束 并不会直接用于frame的计算 而是会转化为对ContentSize的计算

   UIScrollView *scrollview=[[UIScrollView alloc]init];
   [self.view addSubview:scrollview];
   [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(self.view);
   }];
   //1.初始化一个view容器
   UIView *contentview=[[UIView alloc]init];
   [scrollview addSubview:contentview];

   //2、这里的约束并不会转化成frame,而是决定了contantsize
   [contentview mas_makeConstraints:^(MASConstraintMaker *make) {
       //告诉scrollview的滚动内容是以contentview的边界为界限的
       make.edges.equalTo(scrollview);
       
       //内容的宽度是375,决定了水平方向可滚动的大小是375。
       make.width.equalTo(@375);
   }];

   UIView *redview=[[UIView alloc]init];
   redview.backgroundColor=[UIColor redColor];
   [contentview addSubview:redview];
   [array addObject:redview];
   
   [redview mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.right.top.equalTo(contentview);
       make.height.equalTo(@1000);
     
   }];
   
   //3、垂直方向可滚动的范围是直到redview的底部
   [contentview mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.equalTo(redview);
   }];
}

相关文章

  • masonry使用技巧

    一、基本用法 1、长度关系和位置关系,不能做比例运算。举几个例子我现在希望子视图的横向中心线(centerY)在高...

  • Masonry使用技巧

    一、相同View水平、竖直方向上排布 //- (void)mas_distributeViewsAlongAxis...

  • masonry使用技巧

    masonry git地址:https://github.com/SnapKit/Masonry 本文主要会讲到m...

  • masonry使用技巧

    1.当你初次设置约束时,使用mas_makeConstraints;当你界面的组件的当前约束会变大或者缩小时,就需...

  • Masonry 使用技巧

    Masonry 是一个轻量级的布局框架, 拥有自己的描述语法, 采用更优雅的链式语法封装自动布局 简洁明了 并具有...

  • Masonry

    Masonry使用方法Masonry的使用Masonry的github地址 本篇文章 Masonry的基本使用方法...

  • Masonry 单个约束的移除(卸载)和安装

    Masonry小技巧门来自 https://github.com/SnapKit/Masonry/issues/1...

  • 10.4 Masonry使用-动画

    Masonry使用-动画 会进行上下缩放 Masonry使用-动画1.png Masonry使用-动画2.png

  • Masonry使用方法

    Masonry的使用 Masonry的github地址 Masonry的基本使用方法 给控件添加约束使用Mason...

  • 第三方库--Masonry的基本使用

    Masonry是目前最流行的AutoLayout框架. 使用: 将Masonry文件包拖入项目 使用Masonry...

网友评论

    本文标题:Masonry使用技巧

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