Masonry 等间隔或等宽高排列多个控件

masonry 目前提供了相应的接口,直接使用即可

1、固定宽高不固定间隔

 /*
      MASAxisType :横排还是坚排
      withFixedItemLength : 控件的宽或高
     leadSpacing: 第一个控件与边缘的间隔
     tailSpacing : 最后一个控件与边缘的间隔
     */
     mas_distributeViewsAlongAxis:(MASAxisType) withFixedItemLength:(CGFloat) leadSpacing:(CGFloat) tailSpacing:(CGFloat)
    

2、固定间隔不固定宽高

 /*
     MASAxisType :横排还是竖排
     withFixedSpacing: 两个控件间隔
     leadSpacing:第一个控件与边缘的间隔
     tailSpacing: 最后一个控件与边缘的间隔
     */
     mas_distributeViewsAlongAxis:(MASAxisType) withFixedSpacing:(CGFloat) leadSpacing:(CGFloat) tailSpacing:(CGFloat)

两个API,分为固定间隔不固定宽高,固定宽高不固定间隔,根据具体需求使用相应的即可。

需要注意的是: 横排的时候要相应设置控件数组的垂直约束,竖排的时候要相应设置控件数字的水平约束。

例1:水平方向排列、固定控件间隔、控件长度不定

NSArray * titles = @[@"首页",@"学习",@"我的"];
    NSMutableArray *btns = [NSMutableArray array];
    for (NSString *title in titles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btns addObject:btn];
        [self.view addSubview:btn];
    }
  // 水平方向排列、固定控件间隔、控件长度不定
    [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:10 tailSpacing:10];
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.centerY.mas_equalTo(self.view);
    }];

例2:水平方向排列、固定控件长度、控件间隔不定

 NSArray * titles = @[@"首页",@"学习",@"我的"];
    NSMutableArray *btns = [NSMutableArray array];
    for (NSString *title in titles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btns addObject:btn];
        [self.view addSubview:btn];
    }
   
   //水平方向排列、固定控件长度、控件间隔不定 //间隔均分
    [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:60 leadSpacing:10 tailSpacing:10];
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];

例3:垂直方向排列、固定控件间隔、控件高度不定

 NSArray * titles = @[@"首页",@"学习",@"我的"];
    NSMutableArray *btns = [NSMutableArray array];
    for (NSString *title in titles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btns addObject:btn];
        [self.view addSubview:btn];
    }
   
    
   //垂直方向排列、固定控件间隔、控件高度不定
    [btns mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:20 leadSpacing:80 tailSpacing:80];
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(100);
    }];

4、垂直方向排列、固定控件高度、控件间隔不定

NSArray * titles = @[@"首页",@"学习",@"我的"];
    NSMutableArray *btns = [NSMutableArray array];
    for (NSString *title in titles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:title forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor redColor];
        [btns addObject:btn];
        [self.view addSubview:btn];
    }
   
    
   //垂直方向排列、固定控件高度、控件间隔不定
    [btns mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:60 leadSpacing:80 tailSpacing:80];
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(100);
    }];

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值