view子视图的快速遍历

通过遍历子视图(需打tag)快速修改view的属性(背景色, frame, 移除等操作)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 414-100-10, 50)];
    view1.backgroundColor = [UIColor redColor];
    //视图添加方法
    [self.view addSubview:view1]; //index 0 红色
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(110, 110, 414-100-10, 50)];
    view2.backgroundColor = [UIColor greenColor];
    //把某个视图插入到某个位置
    [self.view addSubview:view2];// index 1 绿色
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(120, 120, 414-100-10, 50)];
    view3.backgroundColor = [UIColor blueColor];
    //把某个视图插入到某个位置
    [self.view addSubview:view3];//index 2 蓝色
    //通过打tag的方式唯一标识view
    view1.tag=1;
    view2.tag=2;
    view3.tag=3;
    NSArray *subviews = self.view.subviews;
    for (UIView *view in subviews) {
        if (view.tag == 1)
        {
            view.backgroundColor = [UIColor grayColor] ;
        }
        if (view.tag==2){
            view.frame = CGRectMake(200, 200, 414-100-10, 50);
        }
        if (view.tag == 3) {
            //移除
            [view3 removeFromSuperview];
        }
    }
}
@end

//xr 414*896

results matching ""

    No results matching ""