将UINavigationController添加为UIView的子视图 [英] Adding a UINavigationController as a subview of UIView

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

问题描述

我试图在UINavigationController的顶部显示一个UILabel。问题是当我添加UILabel作为UIWindow的子视图时,它不会自动旋转,因为它不是UIViewController的子视图(UIViewController在旋转期间自动处理更新子视图)。

I'm trying to display a UILabel on top of a UINavigationController. The problem is that when I add the UILabel as a subview of UIWindow it will not automatically rotate since it is not a subview of UIViewController (UIViewController automatically handles updating subviews during rotations).

这是我使用的层次结构:

This is the hierarchy I was using:


  • UIWindow


    • UILabel

    • UINavigationController

    我想我可以使用以下层次结构:

    So I was thinking I could use the following hierarchy:


    • UIWindow


      • UIViewController


        • UIView


          • UILabel

          • UINavigationController

          这样,标签可以显示在UINavigationController的顶部,同时自动旋转,因为它是UIViewController的子视图。

          This way the label could be displayed on top of the UINavigationController's bar while also automatically being rotated since it is a subview of UIViewController.

          问题是,当我尝试添加一个UINavigationController作为视图的子视图:

          The problem is that when I try adding a UINavigationController as a subview of a view:

          [myViewController.view addSubview:myNavigationController.view];
          

          它会出现20像素向下。我猜测是因为它认为它需要为状态栏留出空间。但是,由于UINavigationController被放置在一个UIView内部,不覆盖在状态栏的顶部,它不正确地添加另外20像素。换句话说,UINavigationBar的顶部在屏幕的40像素标记,而不是20像素。

          it will appear 20 pixels downwards. Which I'm guessing is because it thinks it needs to make room for the status bar. But, since the UINavigationController is being placed inside a UIView which does not overlay on top of the status bar, it is incorrectly adding an additional 20 pixels. In other words, the top of the UINavigationBar is at the screen's 40 pixel mark instead of at 20 pixels.

          有任何简单的方法,只是移动UINavigationController和所有其元素(例如导航栏,工具栏,根视图控制器)向上20像素?或者让它知道它不应该补偿状态栏?

          Is there any easy way to just shift the UINavigationController and all of its elements (e.g. navigation bar, tool bar, root view controller) up 20 pixels? Or to let it know that it shouldn't compensate for a status bar?

          如果没有,我想我需要使用我上面提到的第一个层次结构,以旋转标签,使其与导航栏的旋转一致。在哪里可以找到有关如何执行此操作的详细信息?

          If not, I guess I would need to use my first hierarchy mentioned above and figure out how to rotate the label so it is consistent with the navigation bar's rotation. Where can I find more information on how to do this?

          注意:通过在导航栏顶部显示标签,我的意思是它应该覆盖在

          Note: by "displaying a label on top of the navigation bar", I mean it should overlay on top of the navigation bar... it can't simply be wrapped in a bar button item and placed as one of the items of the navigation bar.

          推荐答案

          使用此代码似乎工作:

          nav.view.frame = CGRectMake(nav.view.frame.origin.x, nav.view.frame.origin.y - 20,
                                    nav.view.frame.size.width, nav.view.frame.size.height);
          

          我在将导航控制器添加为子视图之前执行了此操作。使用[UIApplication sharedApplication] .statusBarFrame而不是硬编码的20也许是一个好主意。

          I did this before adding the navigation controller as a subview. Using the [UIApplication sharedApplication].statusBarFrame instead of the hard coded 20 would probably be a good idea too.

          我不知道是否是最好的方法

          I'm not sure if it's the best way to do it though.

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

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