1.設置某view到最上層
// 初始化第一個view并添加到當前控制器的view上;
UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];
// 初始化第二個view并添加到當前控制器的view上;
UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
second.backgroundColor = [UIColor greenColor];
[self.view addSubview:second];
// 設置第一個view到最上層
[self.view bringSubviewToFront:first];
2.設置某view到最下層
// 初始化第一個view并添加到當前控制器的view上;
UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];
// 初始化第二個view并添加到當前控制器的view上;
UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
second.backgroundColor = [UIColor greenColor];
[self.view addSubview:second];
// 初始化第三個view并添加到當前控制器的view上;
UIView *third = [[UIView alloc] initWithFrame:CGRectMake(70, 70, 100, 100)];
third.backgroundColor = [UIColor yellowColor];
[self.view addSubview:third];
[self.view sendSubviewToBack:second];
// 設置第二個view到最下層
[self.view sendSubviewToBack:second];
3.設置某view到指定層
// 初始化第一個view并添加到當前控制器的view上;
UIView *first = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];
// 初始化第二個view并添加到當前控制器的view上;
UIView *second = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
second.backgroundColor = [UIColor greenColor];
[self.view addSubview:second];
// 初始化第三個view并添加到當前控制器的view上;
UIView *third = [[UIView alloc] initWithFrame:CGRectMake(30, 70, 100, 100)];
third.backgroundColor = [UIColor yellowColor];
[self.view addSubview:third];
// 設置第一個view在第一層;第二個在第三層;第三個在第四層;第四個在第二層
first.layer.zPosition = 1;? // red
second.layer.zPosition = 3; // green
third.layer.zPosition = 2;? // hello