循环btn上面的视图

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton * btn = [self createBtn:YES andCGRectMake:CGRectMake(100, 100, 100, 100)];
    [btn setTitle:@"长度" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor cyanColor];
    btn.tag = 1;
    [self.view addSubview:btn];
    
    UIButton * btn1 = [self createBtn:YES andCGRectMake:CGRectMake(100, 250, 100, 100)];
    btn1.tag = 2;
    btn1.selected = NO;
    [btn1 setTitle:@"长度长度" forState:UIControlStateNormal];
    
    //循环btn  btn的背景图片不能更改
    /*
    UIImageView * image = [[UIImageView alloc]init];
    image.backgroundColor = [UIColor orangeColor];
    UIImageView * image1 = [[UIImageView alloc]init];
    image1.backgroundColor = [UIColor yellowColor];
    [btn1 setImage:image.image forState:UIControlStateNormal];
    [btn1 setImage:image1.image forState:UIControlStateSelected];
*/
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn1.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn1];
}

-(void)pressBtn:(UIButton*)sender{
     //循环btn btn的背景图片不能更改
    if (sender.tag == 1) {
        for (id obj in sender.subviews) {
            if ([obj isKindOfClass:[UIImageView class]])
            {
                if (sender.selected) {
                    UIImageView* image = (UIImageView*)obj;
                    image.backgroundColor = [UIColor redColor];

                    sender.selected = NO;
                }
                else{
                    UIImageView* image = (UIImageView*)obj;
                    image.backgroundColor = [UIColor grayColor];
                    sender.selected = YES;
                }
            }
        }
    }
    else{
        for (id obj in sender.subviews) {
            if ([obj isKindOfClass:[UIImageView class]])
            {
                if (sender.selected) {
                    UIImageView* image = (UIImageView*)obj;
                    image.backgroundColor = [UIColor redColor];

                    sender.selected = NO;
                }
                else{
                    UIImageView* image = (UIImageView*)obj;
                    image.backgroundColor = [UIColor grayColor];
                    sender.selected = YES;
                    
                }
            }
        }
    }
}
-(UIButton*)createBtn:(BOOL)yes andCGRectMake:(CGRect)frame{
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(btn.titleLabel.frame.size.width/2+btn.frame.size.width/2+20, 30, 30, 30)];
    image.backgroundColor = [UIColor redColor];
    [btn addSubview:image];
    
    return btn;
    
}

原文地址:https://www.cnblogs.com/sayimba/p/5698358.html