美文网首页
iOS for 循环创建九宫格按钮

iOS for 循环创建九宫格按钮

作者: 半夏吖 | 来源:发表于2017-11-24 21:01 被阅读63次

创建一个九宫格视图显示分享类型

- (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];

    }

}

相关文章

网友评论

      本文标题:iOS for 循环创建九宫格按钮

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