美文网首页
masonry 九宫格

masonry 九宫格

作者: gushidekaitou | 来源:发表于2017-08-07 21:53 被阅读6次
    1. 固定宽高,以及间距的情况
    - (void)confUI
    {
        CGFloat w = (ScreenWidth -24)/3;
        UIView *bgview = [[UIView alloc]init];
        [self addSubview:bgview];
        bgview.layer.borderColor = [UIColor blackColor].CGColor;
        bgview.layer.borderWidth = 1.0;
        [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.right.left.equalTo(self);
        }];
        
        
        UIView *lastView = nil;
        for (NSInteger i = 0; i<9; i++) {
            UIView *view = [[ UIView alloc]init];
            [bgview addSubview:view];
            view.backgroundColor = [UIColor colorWithHue:(arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                              brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1.0];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                if (i%3 == 0) {
                    if (lastView) {
                        make.top.equalTo(lastView.mas_bottom).offset(6);
                    }else{
                        make.top.equalTo(bgview).offset(6);
                    }
                    make.left.equalTo(bgview).offset(6);
                }else if (i%3 == 1)
                {
                    make.left.equalTo(lastView.mas_right).offset(6);
                    make.top.equalTo(lastView.mas_top);
                }else{
                    make.left.equalTo(lastView.mas_right).offset(6);
                    make.right.equalTo(bgview.mas_right).offset(-6);
                    make.top.equalTo(lastView.mas_top);
                }
                make.height.equalTo(@(w));
                make.width.equalTo(@(w));
            }];
            lastView = view;
        }
        [lastView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(bgview.mas_bottom);
        }];
    
    }
    
    

    相关文章

      网友评论

          本文标题:masonry 九宫格

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