美文网首页
按钮流水布局

按钮流水布局

作者: William_ | 来源:发表于2018-04-16 14:02 被阅读13次
    - (void)createBtn {
        
        CGFloat spacing = 10;
        
        CGFloat x = 0;
        CGFloat y = 10;
        CGFloat btnH = 40;
        for (int i = 0; i < _arr.count; i ++) {
            UIButton *btn = [UIButton buttonWithType:0];
            [btn setTitle:[_arr objectAtIndex:i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor lightGrayColor];
            btn.titleLabel.font = [UIFont systemFontOfSize:17];
            [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
            
            CGSize titleSize = [[self.arr objectAtIndex:i] boundingRectWithSize:CGSizeMake(MAXFLOAT, btnH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil].size;
            
            CGFloat btnW = titleSize.width + 2 * spacing;
            if (x + btnW > SCREEN_WIDTH) {
                x = 0;
                y += btnH + spacing;
            }
            btn.frame = CGRectMake(x, y, btnW, btnH);
            x += btnW + spacing;
           
        }  
    }
    

    相关文章

      网友评论

          本文标题:按钮流水布局

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