添加 UINavigationController 作为 UIView 的子视图
我正在尝试在 UINavigationController 之上显示 UILabel。问题是,当我将 UILabel 添加为 UIWindow 的子视图时,它不会自动旋转,因为它不是 UIViewController 的子视图(UIViewController 在旋转期间自动处理更新子视图)。
这是我使用的层次结构:
- UIWindow
- UI标签
- UINavigationController
所以我想我可以使用以下层次结构:
- UIWindow
- UIViewController
- UI视图
- UI标签
- UINavigationController
- UI视图
- UIViewController
这样,标签可以显示在 UINavigationController 栏的顶部,同时也可以自动旋转,因为它是 UIViewController 的子视图。
问题是,当我尝试添加 UINavigationController 作为视图的子视图时:
[myViewController.view addSubview:myNavigationController.view];
它将向下显示 20 像素。我猜测是因为它认为需要为状态栏腾出空间。但是,由于 UINavigationController 被放置在 UIView 内,而 UIView 并未覆盖在状态栏顶部,因此它错误地添加了额外的 20 个像素。换句话说,UINavigationBar 的顶部位于屏幕的 40 像素标记处,而不是 20 像素处。
有没有简单的方法可以将 UINavigationController 及其所有元素(例如导航栏、工具栏、根视图控制器)向上移动 20 像素?或者让它知道它不应该补偿状态栏?
如果没有,我想我需要使用上面提到的第一个层次结构并弄清楚如何旋转标签,使其与导航栏的旋转保持一致。我在哪里可以找到有关如何执行此操作的更多信息?
注意:“在导航栏顶部显示标签”,我的意思是它应该覆盖在导航栏顶部......它不能简单地包裹在栏按钮项目中并作为导航栏的项目之一放置导航栏。
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
- UIView
- 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.
The problem is that when I try adding a UINavigationController as a subview of a view:
[myViewController.view addSubview:myNavigationController.view];
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用此代码似乎有效:
我在将导航控制器添加为子视图之前执行了此操作。使用 [UIApplication sharedApplication].statusBarFrame 而不是硬编码的 20 可能也是一个好主意。
我不确定这是否是最好的方法。
Using this code seems to work:
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.
如果您想要一个代表可用内容区域的框架,那么您应该使用:
[[UIScreen mainScreen] applicationFrame]
。当然,这限制了您的顶级视图控制器,使其只能是顶级。所以仍然有点狡猾,但不那么危险了。If you want a frame representing the available content area, then you should just use:
[[UIScreen mainScreen] applicationFrame]
. Of course, this restricts your top-level view controller so that it can only be top level. So still kind of dodgy, but less so.为什么不使用 App Frame 而不是向源添加一些值?我的意思是使用:
作为参考框架,然后做这样的事情:
这个对我有用。
Why don't you use App Frame instead of adding some values to origins? I mean using:
as a reference frame, and do something like this:
This one worked for me.
我实际上也遇到了同样的问题,但设法解决了它。
我注意到我的视图控制器的视图具有正确的框架,但视图控制器的导航栏没有(它的框架原点为 (0,20) )。
将其插入到作为导航控制器的超级视图的视图控制器中:
I had this same problem actually but managed to fix it.
I noticed that my view controller's view had the correct frame, but the view controller's navigation bar did not (it had a frame origin of (0,20) ).
Insert this into the view's controller that is the superview of the navigation controller:
斯威夫特5:
在 UINavigationController 的根视图控制器的
viewDidLoad()
中添加以下行。Swift 5:
add the following line in the
viewDidLoad()
of the root view controller of the UINavigationController.