iOS父子图层关系

如果多个视图添加到同一个父视图上,公共部分,后添加的会覆盖先添加的

  //创建红色视图
  UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
  redView.backgroundColor = [UIColor redColor];
  redView.tag = 10;
  [self.window addSubview:redView];
    
  //创建黄色视图
  UIView * yellowView = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 100, 100)];
  yellowView.backgroundColor = [UIColor yellowColor];
  yellowView.tag = 20;
  [self.window addSubview:yellowView];
    
  //创建绿色视图
  UIView * greenView = [[UIView alloc] initWithFrame:CGRectMake(110, 110, 100, 100)];
  greenView.backgroundColor = [UIColor greenColor];
  greenView.tag = 30;
  [self.window addSubview:greenView];
1.将一个视图的层次设置成最上面
- (void)bringSubviewToFront:(UIView *)view;
 [_window bringSubviewToFront:redView];
2.将某个视图放到最下面
- (void)sendSubviewToBack:(UIView *)view;
  [_window sendSubviewToBack:yellowView];
3.插入视图(如果被插入的视图已经显示在界面上,那么通过插入方法只是改变它的层次关系;如果被插入的视图没有添加到界面上,通过插入方法可以将视图添加到界面上,并且改变层次关系)
  • 将某个视图插入到指定的视图上面
 - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
 [_window insertSubview:greenView aboveSubview:redView];
  • 将某个视图插入到指定视图的下面
 - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
 [_window insertSubview:greenView belowSubview:yellowView];

你可能感兴趣的:(iOS父子图层关系)