UIView视图层次操作

 //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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值