iOS 流布局(淘宝搜索历史样式)

效果图


iOS 流布局(淘宝搜索历史样式)_第1张图片
IMG_2896.PNG
for (int i = 0; i < array.count; i ++) {
        UIButton *button = [[UIButton alloc]init];
        button.backgroundColor = GetColor(240, 240, 240, 1);
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = FONT(15);
        button.tag = 160 + i;
        [button.addTarget:self action:@selector(chooseButton:) forControlEvents:UIControlEventTouchUpInside];
        CGSize contentSize = [ZQTools getNSStringSize:arrayl[i] andViewWight:375 andFont:FONT(15)];//获取文字的长度
        [button setTitleColor:GetColor(153, 153, 153, 1) forState:UIControlStateNormal];
        LayerCornerRadius(button, ADAPTATION_HEIGHT(13.5));//圆角
        [self.viewWithLabel addSubview: button];
        CGFloat x = 10;
        CGFloat y = 5;
        CGFloat width = contentSize.width + 30;
        if (i != 0) {
            UIButton * button1 = [self.viewWithLabel viewWithTag:159 + i];//获取上一个button
            CGFloat nowWidth = button1.frame.origin.x + button1.frame.size.width + contentSize.width + ADAPTATION_WIDTH(11) + ADAPTATION_WIDTH(10) ;//计算当前button所需要的空间
            if (nowWidth > MAINSCREEN_WIDTH) {
//button下移一行
                x = 10;
                y = button1.frame.origin.y + ADAPTATION_HEIGHT(40);
            } else {
                x = button1.frame.size.width + ADAPTATION_HEIGHT(11) + button1.frame.origin.x;
                y = button1.frame.origin.y;
            }
        }
        button.frame = CGRectMake(x, y, width, ADAPTATION_HEIGHT(27));
    }

你可能感兴趣的:(iOS 流布局(淘宝搜索历史样式))