iOS 基于Masonry 循环创建view

循环创建多个view 是在简单不过的事 但是多数时间建立约束时 都是用的init方法 这里记录一下用Masonry 建立约束的方法

  • 先创建装载的“容器”
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY).with.offset(-50);
        make.width.equalTo(@(200));
        make.height.equalTo(@(200));
    }];

    _scrollView.backgroundColor = [UIColor lightGrayColor];
  • 自定义view
    需要循环创建的view 多数情况下是都是传入自定义的view
    这里 就用这个代替
    //此处可以传view数组  
     NSMutableArray * viewARR = [NSMutableArray array];
    for (int a = 0; a < 3; a ++) {
        UIView * arrview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        arrview.backgroundColor = [UIColor whiteColor];
        [viewARR addObject:arrview];
       
    }

  • 建立约束

左右滚动

    UIView * contenview = [UIView new];
    [_scrollView addSubview:contenview];
    [contenview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo (_scrollView);
        make.height.equalTo (_scrollView); 此处没有设置宽的约束
    }];
    
    //关于这个延迟 因为masory创建约束之后 获取的frame 是(0,0,0,0)但是有一个延迟的话 才能获取真是的view的大小  延迟时间改成0 也可以  
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"******%@",contenview);
    });

    UIView * lastView ;
//颜色数组
    NSArray * colors = @[ [UIColor purpleColor] , [UIColor orangeColor],   [UIColor redColor]];
    for (int i = 0; i < viewARR.count ; i++) {
        UIView * view = viewARR[i];
        [contenview addSubview:view];

        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo (contenview.mas_top);
            make.bottom.equalTo (contenview.mas_bottom);
            make.left.equalTo(lastView ? lastView.mas_right: @0).offset(10);
            make.width.equalTo (_scrollView.mas_width);
        }];
        view.backgroundColor = colors[i];
        lastView = view;
        
       //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            NSLog(@"=======%d___%@",i,lastView);
//        });
  }

    [contenview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView.mas_right);
    }];

上下滚动

 UIView * contenview = [UIView new];
    [_scrollView addSubview:contenview];
    [contenview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo (_scrollView);
        make.width.equalTo (_scrollView);
        
    }];
    
    //关于这个延迟 因为masory创建约束之后 获取的frame 是(0,0,0,0)但是有一个延迟的话 才能获取真是的view的大小  延迟时间改成0 也可以
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        NSLog(@"******%@",contenview);
//    });
    
    UIView * lastView ;
    //颜色数组
    NSArray * colors = @[ [UIColor purpleColor] , [UIColor orangeColor],   [UIColor redColor]];
    for (int i = 0; i < viewARR.count ; i++) {
        UIView * view = viewARR[i];
        [contenview addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo (contenview.mas_left);
            make.right.equalTo (contenview.mas_right);
            make.top.equalTo(lastView ? lastView.mas_bottom: @0).offset(10);
            make.height.equalTo (_scrollView.mas_height);
        }];
        view.backgroundColor = colors[i];
        lastView = view;
        
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    NSLog(@"=======%d___%@",i,lastView);
                });
    }
    
    [contenview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.mas_bottom);
    }];

其实就是基于Masonry 和三目运算实现 (lastView ? lastView.mas_right: @0).offset(10)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、前言 关于苹果的布局一直是我比较纠结的问题,是写代码来控制布局,还是使用storyboard来控制布局呢?以前...
    iplaycodex阅读 2,481评论 0 1
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,834评论 0 1
  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,397评论 0 3
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,200评论 1 4
  • 自从初二这个中二年纪入了网络小说这个坑以来,我就发现我从来没爬起来过。 五年期间大部分精力时间都花在通宵达旦看小说...
    好玩的世界阅读 398评论 0 1