Masonry + 九宫格

//Hugging 拥抱优先级
[label1 setContentHuggingPriority:UILayoutPriorityRequired
                          forAxis:UILayoutConstraintAxisHorizontal];
[label2 setContentHuggingPriority:UILayoutPriorityDefaultLow
                          forAxis:UILayoutConstraintAxisHorizontal];

//Compression 扩撑优先级
[label1 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
                                        forAxis:UILayoutConstraintAxisVertical];
[label2 setContentCompressionResistancePriority:UILayoutPriorityRequired
                                        forAxis:UILayoutConstraintAxisVertical];

//multipliedBy 比例
 [smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);

make.height.equalTo(smallView.button.mas_width).multipliedBy(0.5);//宽高比2:1相对于同一个控件
}];

 

#define BUTTON_SPACING 10 //按钮间距
#define BORDER_SPACING 10 //边框间距
#define BUTTON_NUMS 3  //每行按钮个数

-(void)setButtonArray:(NSArray *)buttonArray{
    _buttonArray = buttonArray;

    CGFloat view_width = SCREEN_WIDTH-30*2;
    CGFloat button_width = (view_width-(BUTTON_NUMS-1)*BUTTON_SPACING-BORDER_SPACING*2)/BUTTON_NUMS;
    CGFloat button_height =  button_width*0.5;//宽高 2:1
    
    for(int i=0 ; i<9; i++){
        UIButton* button = [[ZKButton alloc] init];
        button.backgroundColor = [UIColor blackColor];
        [self.btnView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(BORDER_SPACING+(i/BUTTON_NUMS)*(BUTTON_SPACING+button_height));
            make.left.mas_offset(BORDER_SPACING+(i%BUTTON_NUMS)*(BUTTON_SPACING+button_width));
            make.width.mas_equalTo(button_width);
            make.height.mas_equalTo(button_height);
        }];
        
        __weak typeof(self)wself = self;
        if (i==9-1) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(wself.btnView.mas_bottom).offset(-BORDER_SPACING);
            }];
        }
    }
}
九宫格

 

posted @ 2019-09-06 12:54  &#127810;浪迹天涯&#127810;  阅读(267)  评论(0编辑  收藏  举报