iOS常用

iOS 开发循环创建布局

2020-07-26  本文已影响0人  善良的皮蛋
- (void)shareView 
{
    // 存放按钮九宫格的容器 view
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectZero];
    buttonView.backgroundColor = [UIColor whiteColor];
    buttonView.frame = CGRectMake(0.0, 0.0, Main_Screen_Width, 120.0);
    [self addSubview:buttonView];
    
    int count = 3; // 每行按钮的数量为 3
    CGFloat btnWidth = Main_Screen_Width / count;  //宽
    CGFloat btnHeight = 60.0; //高

    for (int i = 0; i < self.dataArray.count; i++) {
        int row = i / count; // 行
        int col = i % count; // 列

        // 分享类型按钮
        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareButton setImage:[UIImage imageNamed:self.dataArray[i]] forState:UIControlStateNormal];
        shareButton.frame = CGRectMake(col * btnWidth, row * btnHeight, btnWidth, btnHeight);
        shareButton.tag = i;
        [shareButton setBackgroundColor:[UIColor whiteColor]];
        [shareButton setTitle:@"" forState:UIControlStateNormal];
        [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [buttonView addSubview:shareButton];

    }

}
// 需要自定义 btn的h,标题颜色、大小,每行个数,imagesize尺寸,上下间距midspacing
- (void)refreshTestDataWithArray:(NSMutableArray *)dataArray rowCount:(NSInteger)rowCount titleColor:(nonnull NSString *)titleColor titleFont:(CGFloat)fontStr imageSize:(CGFloat)imagesizeStr midspacing:(CGFloat)midspacingStr btnH:(CGFloat)btnHStr{
    
    self.dataArray = dataArray;
    self.pageCtl.numberOfPages = (self.dataArray.count+rowCount*2-1)/(rowCount*2);
    /**   页数 = (总个数 + 每页最大显示个数 - 1) / 每页显示最大的个数  */
    self.scrllView.contentSize = CGSizeMake(SCREEN_WIDTH*((self.dataArray.count+rowCount*2-1)/(rowCount*2)), 200);
    
        if (dataArray.count >0) {
            /** 遍历 */

//            NSInteger count = 4; //每行四个
            NSInteger count = rowCount;
            
            for (int i = 0; i<dataArray.count; i++) {
                NSInteger col = i%count; //列
                NSInteger row = i/count; //行
                
                NNHLog(@"===名称:%@",dataArray[i][@"categoryName"]);
                
                JXLayoutButton *btn = [JXLayoutButton buttonWithType:UIButtonTypeCustom];
               
                [btn setTitle:dataArray[i][@"categoryName"] forState:UIControlStateNormal];
//                [btn setTitle:[NSString stringWithFormat:@"%ld",i] forState:UIControlStateNormal];
                
                [btn setTitleColor:[UIColor akext_colorWithHex:titleColor] forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:fontStr];
                [btn.imageView sd_setImageWithURL:[NSURL URLWithString:dataArray[i][@"categoryPicurl"]] placeholderImage:[UIImage imageNamed:@""]];
//                [btn setImage:[UIImage imageNamed:dataArray[i][@"pic"]] forState:UIControlStateNormal];
                btn.layoutStyle = JXLayoutButtonStyleUpImageDownTitle;
                btn.imageSize = CGSizeMake(imagesizeStr, imagesizeStr);
                btn.midSpacing = midspacingStr;
                btn.adjustsImageWhenHighlighted = NO;
                
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                [self.scrllView addSubview:btn];
                btn.backgroundColor = [UIColor whiteColor];
                
                btn.tag = 10+i;
                
                CGFloat btn_w = (SCREEN_WIDTH - 10*(count+1))/count;
                CGFloat btn_h = btnHStr;
                CGFloat s_w = SCREEN_WIDTH;
                
                if (i>(count*2-1)) {
                    btn.frame = CGRectMake(10+(col)*(btn_w+10)+s_w*(i/8), (row-2*(i/8))*(btn_h+10)+20, btn_w, btn_h);
                    
                }else{
                    
                    btn.frame = CGRectMake(10+col*(btn_w+10), row*(btn_h+10)+20, btn_w, btn_h);
                }
            }

        }
}
上一篇下一篇

猜你喜欢

热点阅读