循环创建按钮

#import "UserCell.h"

#define kBorderX  5  // 左边

#define kBorderY  0 // 上边

#define kMargin  5

#define kImageViewW  ([UIScreen mainScreen].bounds.size.width - kBorderX * 2 - kMargin * 6 ) / 7

@interface UserCell ()

@property (nonatomic, strong) NSMutableArray *imageViewArr;

@end

@implementation UserCell

- (void)awakeFromNib {

    [super awakeFromNib];

    // Initialization code

    [self.imageViewArr removeAllObjects];

    for (NSInteger i = 0; i < 7; i++) {

        CGFloat x = kBorderX + (kMargin + kImageViewW) * i;

        UIImageView *imageView = [[UIImageView alloc] init];

        imageView.frame = CGRectMake(x, kBorderY, kImageViewW, kImageViewW);

        imageView.backgroundColor = [UIColor redColor];

        imageView.userInteractionEnabled = YES;

        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)]];

        imageView.layer.cornerRadius = kImageViewW * 0.5;

        imageView.layer.masksToBounds = YES;

        [self.imageViewArr addObject:imageView];

        [self.contentView addSubview:imageView];

        

    }

}

1
原文地址:https://www.cnblogs.com/fantasy3588/p/5352210.html