View切换层级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self createSubViews];

}

-(void)createSubViews{

UILabel *redLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
redLabel.backgroundColor = [UIColor redColor];
redLabel.text = @"红色";
[self.view addSubview:redLabel];

UILabel *greenLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
greenLabel.backgroundColor = [UIColor greenColor];
greenLabel.text = @"绿色";
[self.view addSubview:greenLabel];

UILabel *blueLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 80, 100, 100)];
blueLabel.backgroundColor = [UIColor blueColor];
blueLabel.text = @"蓝色";
[self.view addSubview:blueLabel];

// 把子视图提到最前面
// [self.view bringSubviewToFront:redLabel];

// 把子视图放到最底层
// [self.view sendSubviewToBack:blueLabel];

// 把某一个子视图放到第0层
// [self.view insertSubview:blueLabel atIndex:0];

// 把blueLabel 放到greenLabel 下面
// [self.view insertSubview:blueLabel belowSubview:greenLabel];

// 把blueLabel放置到redLabel 上面
// [self.view insertSubview:redLabel aboveSubview:blueLabel];

NSLog(@"subViews = %@",self.view.subviews);

}

@end