masonry 九宫格

  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);
    }];

}

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

推荐阅读更多精彩内容