如何将 UIViewController 的视图添加为子视图 [英] How to add an UIViewController's view as subview

查看:29
本文介绍了如何将 UIViewController 的视图添加为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌员工请注意,此问答现已过时六年

Note to googlers, this Q-A is now six years out of date!

正如下面 Micky 和其他人所提到的,现在每天都可以使用 iOS 中的容器来完成.

As Micky below and others mention, this is now done on an everyday basis with Containers in iOS.

我有一个 ViewController 来控制许多子视图.当我单击其中一个按钮时,我会初始化另一个视图控制器并将它的视图显示为该视图的子视图.但是子视图超出了子视图的框架边界,实际上填满了整个屏幕.

I have a ViewController which controls many subviews. When I click one of the buttons I initialize another viewcontroller and show it's view as the subview of this view. However the subview exceeds the bounds of the frame for subview and infact fills the entire screen.

可能有什么问题?我认为问题在于 UIViewController 的视图有一个框架 (0,0,320,460),因此填满了整个屏幕(尽管它仅在子视图框架边界内触摸时才会接收触摸事件).如何调整框架大小以适合子视图.

What could be wrong? I presume the problem is that UIViewController's view has a frame (0,0,320,460) and hence fills the entire screen (though it receive's touch events only when touched within the subview frame bounds). How can I resize the frame to fit as subview.

简而言之,我需要帮助将一个视图控制器的视图作为子视图添加到另一个视图控制器的视图中.

In short, I need help adding a viewcontroller's view as a subview to another viewcontroller's view.

谢谢!

推荐答案

从 iOS 5 开始,Apple 现在允许您制作 自定义容器,用于将 UIViewController 添加到另一个 UIViewController,特别是通过诸如 addChildViewController 所以确实可以嵌套UIViewControllers

As of iOS 5, Apple now allows you to make custom containers for the purpose of adding a UIViewController to another UIViewController particularly via methods such as addChildViewController so it is indeed possible to nest UIViewControllers

包括就地摘要以避免链接中断

Including in-place summary so as to avoid link breakage

我引用:

iOS 提供了许多标准容器来帮助您组织应用.但是,有时您需要创建与任何系统容器提供的工作流都不匹配的自定义工作流.也许在您的愿景中,您的应用需要一个特定的子视图控制器组织,在它们之间具有专门的导航手势或动画过渡.为此,您需要实现一个自定义容器 - 告诉我更多...

iOS provides many standard containers to help you organize your apps. However, sometimes you need to create a custom workflow that doesn’t match that provided by any of the system containers. Perhaps in your vision, your app needs a specific organization of child view controllers with specialized navigation gestures or animation transitions between them. To do that, you implement a custom container - Tell me more...

...和:

设计容器时,您会在容器、父级和其他视图控制器及其子级之间创建明确的父子关系 - 告诉我更多

示例(由 Apple 文档提供)将另一个视图控制器的视图添加到容器的视图层次结构

Sample (courtesy of Apple docs) Adding another view controller’s view to the container’s view hierarchy

- (void) displayContentController: (UIViewController*) content
{
   [self addChildViewController:content];                 
   content.view.frame = [self frameForContentController]; 
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];          
}

这篇关于如何将 UIViewController 的视图添加为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆