iOS 容器控制器 (Container View Controller)
iOS 容器控制器 (Container View Controller)
一个控制器包含其他一个或多个控制器,前者为容器控制器 (Container View Controller),后者为子控制器 (Child View Controller)。UINavigationController、UITabBarController 是常用的容器控制器。本文介绍自定义容器控制器的方法。
自定义容器控制器
添加子控制器
- (void)displayContentController:(UIViewController *)content {
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self];
}
注意,容器控制器的 addChildViewController: 方法会调用子控制器的 willMoveToParentViewController: 方法,因此不需要写子控制器的 willMoveToParentViewController: 方法。
移除子控制器
- (void)hideContentController:(UIViewController *)content {
[content willMoveToParentViewController:nil];
[content.view removeFromSuperview];
[content removeFromParentViewController];
}
注意,子控制器的 removeFromParentViewController 方法会调用 didMoveToParentViewController: 方法,不用写 didMoveToParentViewController: 方法。
子控制器之间的转变
- (void)cycleFromViewController:(UIViewController *)oldVC
toViewController:(UIViewController *)newVC {
// Prepare the two view controllers for the change.
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Get the start frame of the new view controller and the end frame
// for the old view controller. Both rectangles are offscreen.
newVC.view.frame = [self newViewStartFrame];
CGRect endFrame = [self oldViewEndFrame];
// Queue up the transition animation.
[self transitionFromViewController:oldVC toViewController:newVC
duration:0.25 options:0
animations:^{
// Animate the views to their final positions.
newVC.view.frame = oldVC.view.frame;
oldVC.view.frame = endFrame;
}
completion:^(BOOL finished) {
// Remove the old view controller and send the final
// notification to the new view controller.
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
}];
}
容器控制器的 transitionFromViewController:toViewController:duration:options:animations:completion: 方法将 newVC 的 view 添加进来,执行动画 animations block,动画结束就移除 oldVC 的 view。
通知子控制器的出现和消失
- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
return NO;
}
如果加上这一句,容器控制器就要在子控制出现和消失时通知子控制器,分别通过调用子控制器的 beginAppearanceTransition:animated: 方法和 endAppearanceTransition() 方法实现,不要直接调用子控制器的 viewWillAppear:、viewDidAppear:、viewWillDisappear:、viewDidDisappear: 方法。
委托子控制器
重载 childViewControllerForStatusBarStyle 属性,返回相应的子控制器,让子控制器决定状态栏样式。当这个属性发生变化,调用 setNeedsStatusBarAppearanceUpdate() 方法更新状态栏样式。
容器控制器可以用子控制器的 preferredContentSize 属性决定子控制器 view 的大小。
第三方容器控制器
ViewDeck
https://github.com/ViewDeck/ViewDeck
左右侧滑视图,实现侧滑菜单功能。
SWScrollViewController
https://github.com/Silence-GitHub/SWScrollViewController
Scroll view 里加入子控制器的视图,能左右滑动切换子控制器。
SWSegmentedController
https://github.com/Silence-GitHub/SWSegmentedController
通过 UISegmentedControl 切换子控制器。
转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6370049.html
最新文章
- IOS开发基础知识--碎片21
- Codeigniter整合Tank Auth权限类库的教程
- GPIO 配置之ODR, BSRR, BRR 详解
- PHPStorm 2016.2 - 2016.3许可证服务器
- Page类成员
- HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
- Visual Assist X 快捷键
- linux下安装一款笔记软件(为知笔记)
- 2018-2019-2 网络对抗技术 20165328 Exp3 免杀原理与实践
- VC调用外部程序
- 20175314 实验三 敏捷开发与XP实践
- C# DataTable抽取Distinct数据(不重复数据)[z]
- PyCharm 中使用 Pylint 控制代码质量
- Tomcat异常及解决办法——持续更新中
- 自建yum仓库yum源
- CentOS上安装seafile
- B1030 完美数列 (25 分)
- Token以及签名signature的设计与实现
- Hype cycle(Gartner 成熟度曲线)
- webpack 安装以及使用
热门文章
- Hibernate-----关系映射 重点!!!
- (中等) UESTC 360 Another LCIS ,线段树+区间更新。
- :after和:before中的content(放入icon)
- lPC1788驱动SDRAM
- 设计模式笔记之二:Android开发中的MVP架构(转)
- 《算法导论》习题2.3-5 二分搜索 Binary Search
- STM32驱动DS18B20
- view添加阴影无效
- iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏
- Tsinsen-1486:树【Trie树 + 点分治】