您的位置:首页 > 大数据 > 人工智能

初识Container View Controller

2016-05-17 13:31 357 查看

0x00 前言

Container view controller可以将多个ViewController放到一个界面上显示,如UINavigationController、UITabBarController、UISplitViewController等系统控件就是用它实现的。还可以实现一些自定义菜单,如网易新闻新闻分类顶部bar、qq的侧滑效果等。

0x01 添加Child View Controller

根据苹果官方文档所述,添加步骤如下:

Call the addChildViewController: method of your container view controller.This method tells UIKit that your container view controller is now managing the view of the child view controller.

Add the child’s root view to your container’s view hierarchy. Always remember to set the size and position of the child’s frame as part of this process.

Add any constraints for managing the size and position of the child’s root view.

Call the didMoveToParentViewController: method of the child view controller.

调用addChildViewController:告诉UIKit子视图(child view controller)和父容器(container view controller)的关系,建立父子关系;

将子视图的根view添加到父容器view中,并指定子视图的位置和大小;

设置子视图的约束;

调用didMoveToParentViewController:,通知子视图父视图发生改变。

addChildView三步曲

// 1.设置vc为子视图,调用时会自动调用willMoveToParentViewController。
[self addChildViewController:vc];
// 2.子vc的根view添加到父vc的view中,并且设置它的位置和大小
[self.view addSubview:vc.view];
[vc.view setFram:self.view.bounds];
// 3.通知子vc,父vc发生改变。
[vc didMoveToParentViewController:self];


需要注意的是,代码里面没有写willMoveToParentViewController:是因为addChildViewController:会自动调用它。需要手动调用didMoveToParentViewController:是因为该方法不能被调用直到子视图的根view添加到父容器中。

0x02 移除Child View Controller

移除步骤如下:

Call the child’s willMoveToParentViewController: method with the value nil.

Remove any constraints that you configured with the child’s root view.

Remove the child’s root view from your container’s view hierarchy.

Call the child’s removeFromParentViewController method to finalize the end of the parent-child relationship.

调用willMoveToParentViewController:参数为nil;

移除子视图view的约束

移除子视图view

调用removeFromParentViewController来确定它们父子关系的结束

removeChildView三步曲

// 1.在将要移除vc时调用willMoveToParentViewController
[vc willMoveToParentViewController:nil];
// 2.移除vc的view
[vc.view removeFromSuperview];
// 3.解除子vc和父vc父子关系,调用时会自动调用didMoveToParentViewController
[vc removeFromParentViewController];


需要注意的是,调用removeFromParentViewController会自动调用didMoveToParentViewController。

0x03 子视图转场

步骤:

旧控制器调用willMoveToParentViewController,通知它将要离开父控制器;

调用addChildViewController,将新控制器设置为父控制器的子控制器;

调用transitionFromViewController:toViewController:duration:options:animations:completion:,在animations设置动画,在completion时移除子控件,这个函数会自动更新父控制器的view,因此不用手动添加或移除view。

// 1.旧控制器调用willMoveToParentViewController,通知它将要离开父控制器
[oldVC willMoveToParentViewController:nil];

// 2.  调用addChildViewController,将新控制器设置为父控制器的子控制器
[self addChildViewController:newVC];

// 3. 调用动画
[self transitionFromViewController: oldVC toViewController: newVC
duration: 1 options:0
animations:^{
newVC.view.frame = oldVC.view.frame;
oldVC.view.frame = [self oldViewEndFrame];
}
completion:^(BOOL finished) {
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
}];


0x04 自定义子视图Appear时机

默认情况下,子视图的Appear会自动调用,重写shouldAutomaticallyForwardAppearanceMethods方法把返回值设置为NO(默认YES)即可自定义子视图的Appear时机。

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
// 自定义Appear时机
return NO;
}

-(void) viewWillAppear:(BOOL)animated {
// 调用子视图的viewWillAppear
[self.child beginAppearanceTransition: YES animated: animated];
}

-(void) viewDidAppear:(BOOL)animated {
// 调用子视图的viewDidAppear
[self.child endAppearanceTransition];
}

-(void) viewWillDisappear:(BOOL)animated {
// 调用子视图的viewWillDisappear
[self.child beginAppearanceTransition: NO animated: animated];
}

-(void) viewDidDisappear:(BOOL)animated {
// 调用子视图的viewDidDisappear
[self.child endAppearanceTransition];
}


本文涉及到的Demo:https://github.com/myzlhh/ContainerViewControllerDemo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: