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

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

问题描述

对于googlers来说,这个QA现在已经过时了六年

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 。当我单击其中一个按钮时,我初始化另一个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.

简而言之,我需要帮助将viewcontroller的视图作为子视图添加到另一个viewcontroller的视图。

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

谢谢!

推荐答案

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

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...

...和:


设计容器时,在容器,父容器和其他视图控制器及其子控件之间创建显式父子关系 - < a href =https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html\"rel =noreferrer>告诉我更多信息

示例(由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];          
}

干杯,

Micky Duncan

Micky Duncan

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

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