直接上代码
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 50)];
- view1.backgroundColor = [UIColor blueColor];
- [self.view addSubview:view1];
- UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(15, 55, 100, 50)];
- view2.backgroundColor = [UIColor grayColor];
- [self.view addSubview:view2];
- //如果将下面两行代码都注释掉 view1 会在下面 view2会在上面
- // 下面这行代码能够将view2 调整到父视图的最下面
- // [self.view sendSubviewToBack:view2];
- //将view调整到父视图的最上面
- [self.view bringSubviewToFront:view1];
- }
这篇博客介绍了在iOS开发中如何使用`sendSubviewToBack:`和`bringSubviewToFront:`方法来管理视图的层次关系。通过示例代码展示了这两个方法如何使视图在叠加时改变前后顺序,`sendSubviewToBack:`将指定视图移至最底层,而`bringSubviewToFront:`则将其移至最顶层。
1万+

被折叠的 条评论
为什么被折叠?



