iOS开发从入门到精通--UIView层级关系

UIView层级关系

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //创建三个视图对象
    UIView * view01 = [[UIView alloc]init];
    view01.frame=CGRectMake(100, 100, 150, 150);
    view01.backgroundColor=[UIColor blueColor];

    UIView * view02 = [[UIView alloc]init];
    view02.frame=CGRectMake(125, 125, 150, 150);
    view02.backgroundColor=[UIColor orangeColor];

    UIView * view03 = [[UIView alloc]init];
    view03.frame=CGRectMake(150, 150, 150, 150);
    view03.backgroundColor=[UIColor greenColor];

    //将三个视图对象显示在屏幕上
    //并且添加到父亲视图上
    //哪一个视图被先添加到父亲视图中,就先绘制哪一个视图
    //哪一个视图被最后一个添加到父亲视图中,就最后绘制哪一个视图
    [self.view addSubview:view01];
    [self.view addSubview:view02];
    [self.view addSubview:view03];

    //将某一个视图调整到最前面显示
    //参数:UIView对象,调整哪一个视图到最前方
    [self.view bringSubviewToFront:view01];

    //将某一个视图调整到最后面显示
    //参数:UIView对象,调整哪一个视图到最后方
    [self.view sendSubviewToBack:view03];

    //subview管理所有的self.view的子视图的数组
    UIView * viewsub = self.view.subviews[0];

    //此判断的依据是根据在父亲视图的前后顺序来的,最下面的是下标为0
    if(view03==viewsub){
        NSLog(@"是同一个view");
    }else{
        NSLog(@"不是一个view");
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(ios开发,ios开发从入门到精通)