iOS开发-视图层次关系、事件触发

iOS 设置view的层级关系

2017-01-19  本文已影响3683人  小朤

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

上一篇下一篇

猜你喜欢

热点阅读