Masony 循环添加控件

__block UILabel *lastLabel = nil;
    WEAKSELF
    for (int i = 0; i < self.categoryArr.count; i++) {
        UILabel *label = [UILabel new];
        label.tag = 1000 + i;
        if (i == 0)
            label.textColor = kAppThemeColor;
        else
            label.textColor = kAppThemeWordColor;
        label.font = kAppThemeFont;
        label.text = [weakSelf.categoryArr objectAtIndex:i];
        label.userInteractionEnabled = YES;
        label.textAlignment = NSTextAlignmentCenter;
         UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionForCategory:)];
        [label addGestureRecognizer:recognizer];
        [self.scrollView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(57);
            make.height.mas_equalTo(32);
            make.top.equalTo(weakSelf.scrollView.mas_top);
            if (!lastLabel)
                make.left.equalTo(weakSelf.scrollView.mas_left);
            else
                make.left.equalTo(lastLabel.mas_right);
        }];
        lastLabel = label;
    }

你可能感兴趣的:(Masony 循环添加控件)