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

查看:30
本文介绍了添加一个 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:

  • 界面窗口
    • UILabel
    • UINavigationController

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

    So I was thinking I could use the following hierarchy:

    • 界面窗口
      • UIViewController
        • 界面视图
          • 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天全站免登陆