【问题标题】:How to add a UIViewController as a subview to a UIViewController(RootViewController)?如何将 UIViewController 作为子视图添加到 UIViewController(RootViewController)?
【发布时间】:2012-06-18 17:23:42
【问题描述】:

我有一个名为LoginViewControllerUIViewController,它是AppDelegate 中的rootViewController。在LoginViewController 中,我有两个按钮:LoginEnroll

当我点击Login 时,我将TabBarController 指定为rootViewController,然后显示TabBarController。但是,现在我想当我点击Enroll 时,我需要添加另一个UIViewcController 作为子视图。我尝试了以下方法:

[self.view addsubview:viewcontroller.view];

但这里的问题是我的 ViewController 的 view.top 被固定在屏幕顶部下方约 20 像素处。我认为状态栏有问题,但我不知道如何解决它。

我认为我需要将我的 ViewController 作为子视图添加到 LoginViewController,然后从那里重定向到不同的视图。有人可以建议其他选择吗?

【问题讨论】:

    标签: iphone ios uiviewcontroller addsubview


    【解决方案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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 2020-02-11
        • 2011-10-02
        • 2017-01-26
        相关资源
        最近更新 更多