美文网首页
Masonary循环自动添加

Masonary循环自动添加

作者: 我不白先生 | 来源:发表于2020-11-04 22:34 被阅读0次
    image.png

    ViewController.m

    #import "ViewController.h"
    #define MAS_SHORTHAND_GLOBALS
    #import "Masonry.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    -(UIColor*)randomColor{
        CGFloat red = rand() % 256 / 255.0;
        CGFloat green = rand() % 256 / 255.0;
        CGFloat blue= rand() % 256 / 255.0;
        return [UIColor colorWithRed:red green:green blue:blue alpha:1];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
         //Do any additional setup after loading the view, typically from a nib.
        UIView *lastView = nil;
        for (NSInteger i = 0; i< 10; i++) {
            UIView *view = [UIView new];
            view.backgroundColor = [self randomColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                //垂直方向居中
                make.centerY.equalTo(0);
                //高度等于宽度
                make.height.equalTo(view.mas_width);
                if(i == 0){
                    make.left.equalTo(0);
                }
                if(i == 9){
                    make.right.equalTo(0);
                }
                if(i != 0){
                 make.left.equalTo(lastView.mas_right);
                    make.width.equalTo(lastView);
                }
            }];
            lastView = view;
        }
    }
    
    image.png
    • 练习
      屏幕效果


      image.png
    image.png

    ViewController.m

    #import "ViewController.h"
    #define MAS_SHORTHAND_GLOBALS
    #import "Masonry.h"
    @interface ViewController ()
    @end
    
    @implementation ViewController
    -(UIColor*)randomColor{
        CGFloat red = arc4random() % 256 / 255.0;
        CGFloat green = arc4random() % 256 / 255.0;
        CGFloat blue = arc4random() % 256 / 255.0;
        return [UIColor colorWithRed:red green:green blue:blue alpha:1];
    }
    -(void)createView:(NSInteger)count{
        //断言 如果第一个参数的条件 满足,程序继续向下执行。如果不满足,则程序崩掉,并提示后面的错误信息
        NSAssert(count <= 2000,@"无法对2000以上进行操作");
        UIView *lastView = nil;
        for(NSInteger i = 0; i< count; i++){
            UIView* view = [[UIView alloc]init];
            view.backgroundColor = [self randomColor];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self.view);
                if(i==0){
                    make.top.equalTo(0);
                }else{
                    make.height.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom);
                    if(i==count-1){
                        make.bottom.equalTo(0);
                    }
                }
            }];
            lastView = view;
        }
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self createView:300];
    }
    
    

    滚动视图加约束Masonary
    ViewController.m

    #import "ViewController.h"
    #define MAS_SHORTHAND_GLOBALS
    #import "Masonry.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    -(UIColor*)randomColor{
        CGFloat red = arc4random() % 256 / 255.0;
        CGFloat green = arc4random() % 256 / 255.0;
        CGFloat blue = arc4random() % 256 / 255.0;
        return [UIColor colorWithRed:red green:green blue:blue alpha:1];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIScrollView *sv = [UIScrollView new];
        [self.view addSubview:sv];
        [sv mas_makeConstraints:^(MASConstraintMaker *make) {
            //设置滚动视图全屏
            make.edges.equalTo(0);
        }];
         UIView *lastView = nil;
        for(NSInteger i = 0;i<6;i++){
            UIView *view = [UIView new];
            view.backgroundColor = [self randomColor];
            [sv addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(0);
                make.size.equalTo(sv);
                if (i == 0) {
                    make.left.equalTo(0);
                }else{
                    make.left.equalTo(lastView.mas_right);
                    if (i == 5) {
                        make.right.equalTo(0);
                    }
                }
            }];
            lastView = view;
        }
        sv.pagingEnabled = YES;
    }
    

    相关文章

      网友评论

          本文标题:Masonary循环自动添加

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