UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(130, 100, 100, 100)];
view2.backgroundColor = [UIColor blackColor];
[self.view addSubview:view2];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(150, 100, 100, 100)];
lable.backgroundColor = [UIColor greenColor];
[self.view addSubview:lable];
正常顺序执行应该是这样子:
想让红色view在最上层显示则:
[self.view bringSubviewToFront:view1];
如果想让黑色在最下边显示则:
[self.view sendSubviewToBack:view2];
还有一些方法请自行实验:
// 添加视图
addSubview:
//将一个视图移到前面
bringSubviewToFront:
//将一个视图推送到背后
sendSubviewToBack:
//把视图移除
removeFromSuperview
//插入视图 并指定索引
insertSubview:atIndex:
//插入视图在某个视图之上
insertSubview:aboveSubview:
//插入视图在某个视图之下
insertSubview:belowSubview:
//交换两个位置索引的视图
exchangeSubviewAtIndex:withSubviewAtIndex:
//当加入视图完成后调用
(void)didAddSubview:(UIView *)subview
//当视图移动完成后调用
(void)didMoveToSuperview
//当视图移动到新的WINDOW后调用
(void)didMoveToWindow
//在删除视图之后调用
(void)willRemoveSubview:(UIView *)subview
//当移动视图之前调用
(void)didMoveToSuperview:(UIView *)subview
//当视图移动到WINDOW之前调用
(void)didMoveToWindow
网友评论