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 容器控制器 (Container View Controller)的更多相关文章

  1. iOS Container View Controller

    一.UIViewController 做iOS开发的经常会和UIViewController打交道,从类名可知UIViewController属于MVC模型中的C(Controller),说的更具体点 ...

  2. Swift - iOS中各种视图控制器(View Controller)的介绍

    在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...

  3. 关于报错:'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based

    最近在看Extension相关知识的时候,自己写了个小demo 发现[UIApplication sharedApplication]这个方法敲不出来了, 总是报错:'sharedApplicatio ...

  4. iOS9中找不到XXX.dylib 与 is unavailable no availabel on ios (app extension) - use view controller 的解决办法

    在 iOS9 中现在找不到 XXX.dylib 了,比如libz.tbd  如果要用到 libz.dylib,可以用下面的办法,来自 Stack Overflow. Go to Build Phase ...

  5. iOS架构师之路:控制器(View Controller)瘦身设计

    前言 古老的MVC架构是容易被iOS开发者理解和接受的设计模式,但是由于iOS开发的项目功能越来越负责庞大,项目代码也随之不断壮大,MVC的模糊定义导致我们的业务开发工程师很容易把大量的代码写到视图控 ...

  6. 《iOS Human Interface Guidelines》——Container View Controller

    容器视图控制器 容器视图控制器管理和展示它的子视图集合--或者子控制器集合--以一种自己定义的方式. 系统定义的容器视图控制器的样例有标签栏视图控制器.导航栏视图控制器和分栏视图控制器(查看Tab B ...

  7. Container View Controller

    有时候,我们的Controler中包含有另一个controler view的view时,可以使用这种方式. https://developer.apple.com/library/ios/featur ...

  8. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  9. View Controller容器

    在 iOS 5 之前,view controller 容器是 Apple 的特权.实际上,在 view controller 编程指南中还有一段申明,指出你不应该使用它们.Apple 对 view c ...

随机推荐

  1. Android系统属性SystemProperties分析

    下面这几个博客总结的不错,有空看下: http://www.cnblogs.com/bastard/archive/2012/10/11/2720314.html http://blog.csdn.n ...

  2. RealView编译器常用特有功能(转)

    源:RealView编译器常用特有功能 一. 关键字和运算符 1. __align(n):指示编译器在n 字节边界上对齐变量. 对于局部变量,n 值可为 1.2.4 或 8. 对于全局变量,n 可以具 ...

  3. CodeForces 631B Print Check

    对于每一个格子,看是行最后画还是列最后画.预处理一下就可以了. #include<stdio.h> #include<string.h> int n,m,k; +]; +]; ...

  4. POJ3255次短路

    POJ3255 题意:给定一个图,求从1到n的次短路 分析:我们需要在dijkstra上作出一些修改,首先,到某个顶点v的次短路要么是到其他某个顶点u的最短路在加上u到v的边,要么是到v的次短路再加上 ...

  5. displayport-2

    上一章讲述了display-port的硬件连接,今天来说说协议层 图中可以看到,最底层是物理层,上层是连接服务层,提供的服务包括同步数据传输服务,aux链接服务,aux设备数据传输服务,在设备端也一样 ...

  6. ios 自定义NSError

    from:[object-c错误处理]http://www.androiddev.net/objective-c%E5%AD%A6%E4%B9%A0%E4%B9%8B%E9%94%99%E8%AF%A ...

  7. BZOJ2064: 分裂

    2064: 分裂 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 360  Solved: 220[Submit][Status][Discuss] De ...

  8. redhat在线安装chrome浏览器

    开始的时候是参考吹尽黄沙始到金的文章http://www.cnblogs.com/effective/archive/2012/03/18/2405189.html 1.创建一个文件/etc/yum. ...

  9. java系列--I/O流

    java.io类 一.I/O流 1.流的概念 二. InputStream --此抽象类是表示字节输入流的所有类的超类. 1.public int read(byte[] b) throws IOEx ...

  10. bzoj 1036

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11858  Solved: 4803[Submit ...