创建子视图控制器

#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

你可能感兴趣的:(创建子视图控制器)