masonry 九宫格

2017-08-07  本文已影响6人  gushidekaitou
  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);
    }];

}

上一篇下一篇

猜你喜欢

热点阅读