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三步曲