创建子视图控制器

#import "BaseViewController.h"

#import "ViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {

[super viewDidLoad];

ViewController * v1= [[ViewController alloc]init];

ViewController * v2= [[ViewController alloc]init];

ViewController * v3= [[ViewController alloc]init];

ViewController * v4= [[ViewController alloc]init];

v1.view.backgroundColor = [UIColor redColor];

v2.view.backgroundColor = [UIColor cyanColor];

v3.view.backgroundColor = [UIColor grayColor];

v4.view.backgroundColor = [UIColor greenColor];

// 1、向视图控制器容器中添加子视图控制器

[self addChildViewController:v1];

[self addChildViewController:v2];

[self addChildViewController:v3];

[self addChildViewController:v4];

[v1.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self.view addSubview: v1.view];

[v2.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self.view addSubview: v2.view];

[v3.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self.view addSubview: v3.view];

[v4.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self.view addSubview: v4.view];

//2、交换两个子视图控制器的位置

[self transitionFromViewController:v4 toViewController:v3 duration:5 options:UIViewAnimationOptionLayoutSubviews animations:^{

v3.view.frame = CGRectMake(100, 100, 200, 200);

} completion:^(BOOL finished) {

///v4 被替换掉的视图,动画完成后,显示v3的View,V4的视图 消失

}];

//3、从父视图控制器中删除

[v2 removeFromParentViewController];//并不会移除 已经 添加在 self.view 上的 v2.view

}


参考1

参考2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容