码迷,mamicode.com
首页 > 其他好文 > 详细

Masonry的一些等间距布局

时间:2016-12-01 14:05:16      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:sax   asc   color   set   uiview   uicolor   view   edit   bsp   

  • 控件之间的间距相等,但是控件的宽度是不定的。

  下列的代码:定义间距为10,yellowview的宽度是由redView的宽度计算出来的。

    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.view);
        make.top.mas_equalTo(ws.view);
        make.right.mas_equalTo(ws.view);
    }];
    
    NSInteger padding = 10;
    UIView *yellowView1 = [[UIView alloc] init];
    yellowView1.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView1];
    
    UIView *yellowView2 = [[UIView alloc] init];
    yellowView2.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView2];
    
    UIView *yellowView3 = [[UIView alloc] init];
    yellowView3.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView3];
    
    [@[yellowView1,yellowView2,yellowView3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:padding leadSpacing:padding tailSpacing:padding];
    
    [@[yellowView1,yellowView2,yellowView3] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(redView).offset(padding);
        make.height.mas_equalTo(yellowView1.mas_width);
        make.bottom.mas_equalTo(redView).offset(-padding);
    }];
  • 控件的宽度是一定的,但是控件之间的间距是不定的。

  下列的代码:定义控件的宽度为22,控件之间的间距是由redView的宽度计算出来的。

    UIView *redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.view);
        make.top.mas_equalTo(ws.view);
        make.right.mas_equalTo(ws.view);
    }];
    
    UIView *yellowView1 = [[UIView alloc] init];
    yellowView1.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView1];
    
    UIView *yellowView2 = [[UIView alloc] init];
    yellowView2.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView2];
    
    UIView *yellowView3 = [[UIView alloc] init];
    yellowView3.backgroundColor = [UIColor yellowColor];
    [redView addSubview:yellowView3];
    
    //控件的宽度
    CGFloat viewW = 22;
    CGFloat padding = (SCREENWIDTH - 3*viewW) * 1.0 / 4;
    
    [@[yellowView1,yellowView2,yellowView3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:viewW leadSpacing:padding tailSpacing:padding];
    
    [@[yellowView1,yellowView2,yellowView3] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(redView).offset(padding);
        make.size.mas_equalTo(viewW);
        make.bottom.mas_equalTo(redView).offset(-padding);
    }];

 

Masonry的一些等间距布局

标签:sax   asc   color   set   uiview   uicolor   view   edit   bsp   

原文地址:http://www.cnblogs.com/iOSDeng/p/6121380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!