您的位置:首页 > 产品设计 > UI/UE

UIView视图层次操作

2014-08-26 10:50 239 查看
//UIView
//hiden     removeFormSuperview               release
//看不见   会被销毁,将该对象从响应者链中删除,      简单的销毁对象
//        并且将view从屏幕上移除

//UIView层次
//view1是window的子视图
//view2是view1的子视图

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
view1.backgroundColor = [UIColor purpleColor];
//裁剪子视图,裁剪子视图超过父视图的区域
view1.clipsToBounds = YES;
//隐藏视图,所有的子视图也会被隐藏
view1.hidden = YES;
[self.window addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
[view1 addSubview:view2];//添加子视图

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(90, 90, 100, 100)];
view3.backgroundColor = [UIColor greenColor];
[self.window addSubview:view3];//添加子视图
//[self.window addSubview:view3]; //最后一次生效

//视图层次操作
//删除view
//[view3 removeFromSuperview]; //将该view从父视图中删除

//插入视图
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(90, 90, 300, 50)];
view4.backgroundColor = [UIColor blueColor];
[self.window insertSubview:view4 atIndex:[self.window subviews].count];
//将view4添加到view3之下
//[self.window insertSubview:view4 belowSubview:view3];
//将view4添加到view3之上
//[self.window insertSubview:view4 aboveSubview:view3];

//交换
//[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

//将view放到最底下
//[self.window sendSubviewToBack:view4];

//将view放到最顶上
[self.window bringSubviewToFront:view4];

//获取所有子视图,一个view可以有多个子视图
NSArray *subs = [self.window subviews];
NSLog(@"subs = %@", subs);
//一个view只能有一个父视图
UIView *superView = [view3 superview];
NSLog(@"superView = %@", superView);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐