如何将 UIViewController 作为子视图添加到 UIViewController(RootViewController)?
Posted
技术标签:
【中文标题】如何将 UIViewController 作为子视图添加到 UIViewController(RootViewController)?【英文标题】:How to add a UIViewController as a subview to a UIViewController(RootViewController)? 【发布时间】:2012-06-18 17:23:42 【问题描述】:我有一个名为LoginViewController
的UIViewController
,它是AppDelegate
中的rootViewController
。在LoginViewController
中,我有两个按钮:Login
和Enroll
。
当我点击Login
时,我将TabBarController
指定为rootViewController
,然后显示TabBarController
。但是,现在我想当我点击Enroll
时,我需要添加另一个UIViewcController
作为子视图。我尝试了以下方法:
[self.view addsubview:viewcontroller.view];
但这里的问题是我的 ViewController 的 view.top 被固定在屏幕顶部下方约 20 像素处。我认为状态栏有问题,但我不知道如何解决它。
我认为我需要将我的 ViewController 作为子视图添加到 LoginViewController,然后从那里重定向到不同的视图。有人可以建议其他选择吗?
【问题讨论】:
【参考方案1】:尝试为您的注册屏幕对象设置框架,然后将其作为子视图添加到登录视图。 例如:
[enrollViewcontroller.view setFrame:CGRectMake(0,0,320,440)];
[self.view addsubview:enrollViewcontroller.view];
【讨论】:
根据您的要求更改框架。因为它向下 20 像素,所以尝试像这样 CGRectMake(0,-20,320,440) 更改 y 轴位置并检查。【参考方案2】:你不应该让 UIViewController 成为另一个 UIViewController 视图的子视图。如果将子视图视为普通 UIView(如果不是这两个视图),那么您可能想要做的事情是屏幕上只有一个 UIViewController 并占据整个屏幕。
更多:How to add an UIViewController's view as subview
【讨论】:
【参考方案3】:我没有将 UIViewController 作为子视图添加到另一个 UIViewController,而是决定使用
将我的 ViewController 呈现为 ModalViewController[self presentModalViewController:myViewController animated:YES];
【讨论】:
以上是关于如何将 UIViewController 作为子视图添加到 UIViewController(RootViewController)?的主要内容,如果未能解决你的问题,请参考以下文章
如何将一个 UIViewController 作为屏幕的一部分
将 UITableView 作为容器视图嵌入到 UIViewController 中
如何最好地将使用一个 UIViewController 将所有页面作为 UIView 处理为 StoryBoarding 和 UIPageView 的图书应用程序转换?