您的位置:首页 > 其它

视图层级管理

2014-05-20 14:10 155 查看
1.一个视图作为父视图时,可以添加多个子视图2.一个视图作为子视图时,只有一个父视图3.新添加(后添加)的子视图,永远在其他同级的子视图的前边(上边)4.父视图使用数组管理子视图subViews5.视图使用树形结构,管理所有子视图6.父视图可以修改已经存在的所有子视图的关系:提前 置后 交换 子视图可以从父视图上移除.以上操作均会影响subviews7.父视图可以直接添加子视图.插入子视图:根据索引,显示在指定子视图上方,或者显示在指定子视图的下边8.viewWithTag:通过tag值搜索本视图及所有子视图,tag值某认为0;

//创建蓝色视图,添加window
UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
blueView.backgroundColor = [UIColor blueColor];
blueView.tag = 100;
[self.window addSubview:blueView];
[blueView release];

//创建蓝色的子视图,红的
UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(70, 70, 50, 50)];
redView.backgroundColor = [UIColor redColor];
[blueView addSubview:redView];
[redView release];
//创建黄色视图,添加到window
UIView * yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
yellowView.backgroundColor = [UIColor yellowColor];
[self.window addSubview:yellowView];
[yellowView release];
//将蓝色视图放在前头
// [self.window bringSubviewToFront:blueView];
//将蓝色视图放在最后头
//[self.window sendSubviewToBack:blueView];
//将蓝色和黄色位置交换
//[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//将蓝色视图删除
//[blueView removeFromSuperview];
//将蓝色视图放在1的后头
//[self.window insertSubview:blueView atIndex:1];
//将蓝色放在黄色前头
[self.window insertSubview:blueView aboveSubview:yellowView];
//插入蓝色在黄色的前头
//[self.window insertSubview:blueView belowSubview:yellowView];
//isDescendantOfView:(UIView *)view方法,判断view是不是指定视图的子视图
[redView isDescendantOfView:blueView];
NSLog(@"subviews = %@",self.window.subviews);

UIView * blackView = [[UIView alloc] initWithFrame:CGRectMake(40, 200, 200, 200)];
blackView.backgroundColor = [UIColor blackColor];
[self.window addSubview:blackView];
[self.window insertSubview:blackView belowSubview:yellowView];
[self.window insertSubview:yellowView aboveSubview:blueView];
[self.window insertSubview:blackView atIndex:1];
//隐藏蓝色视图
//blueView.hidden = YES;
//修改蓝色视图的透明度,父视图的alpha会影响子视图.
blueView.alpha = 0.5;
yellowView.alpha = 0.5;
blackView.alpha = 0.5;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: