ios 使用Masonary创建等间距排列的控件

本文详细介绍了如何在iOS开发中利用Masonry库,通过Swift或Objective-C进行代码布局,实现控件的等间距排列。通过设置约束和更新约束,确保视图在不同屏幕尺寸下保持一致的间距效果。
摘要由CSDN通过智能技术生成
UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(100);
        make.height.mas_equalTo(100);
    }];
    
    NSMutableArray *array = [NSMutableArray array];
    
    
    for(NSInteger i=0;i<5;i++){
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1.0];
        [backView addSubview:view];
        
        [array addObject:view];
    }
    //FixedSpacing:间距
    //leadSpacing:最左边控件距父视图左边间距
    //tailSpacing:最右边控件距父视图右边间距
    //MASAxisTypeVertical 控件垂直排列
    //MASAxisTypeHorizontal 控件水平排列
    [array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:60 tailSpacing:5];
    [array mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.bottom.equalTo(@-10);
    }];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值