在容器视图中,导航控制器的导航栏未调整大小以包含状态栏 [英] In a container view, a navigation controller's navigation bar not resizing to include status bar

查看:29
本文介绍了在容器视图中,导航控制器的导航栏未调整大小以包含状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,它需要在导航栏和其他视图上方有一个栏.为了实现这一点,我使用了一个带有顶部栏视图的视图控制器,然后是其他所有内容的容器视图.有时,顶部栏需要隐藏.我使用自动布局来隐藏顶部栏(将其高度设置为 0),并且容器视图扩展以填充屏幕(容器顶部等于顶部栏底部).容器视图包含一个导航控制器,因为有时我需要顶部栏下方的导航栏.当我启动应用程序时,一切正常,如下所示:

I have created an application which needs to have a bar above the navigation bar and other views. To accomplish this, I am using a view controller with a view for the top bar, and then a container view for everything else. Sometimes, the top bar needs to be hidden. I'm using autolayout to hide the top bar (set its height to 0), and the container view expands to fill the screen (container top equal to the top bar bottom). The container view contains a navigation controller because I need a navigation bar below the top bar sometimes. When I start the app, this all works fine as shown below:

如您所见,导航栏保持所需的高度,展开后包含状态栏

As you can see, the navigation bar stays the desired height, which is expanded to include the status bar

但是,当点击顶部栏时,我使用 segue 从我的根视图控制器(不是容器)中呈现(而不是推送)一个视图控制器,然后我用导航栏查看同一个屏幕,导航栏不再扩展,它与状态栏重叠,如下所示:

However, when the top bar is tapped, I use a segue to present (not push) a view controller from my root view controller (not the container), and then I look at the same screen with the navigation bar, the navigation bar is no longer extended and it overlaps the status bar as seen below:

为什么呈现视图控制器会破坏这一点?那么我该如何纠正或防止它呢?

Why would presenting a view controller break this? And then how could I rectify this or prevent it?

我可以做的一个可能的解决方案是从容器视图中呈现一个视图控制器,这是可行的,我只需要将容器中的当前视图控制器设置为根控制器的委托,这样当顶部栏被点击,它告诉容器中的视图控制器呈现新的视图控制器.然而,这不是我的第一选择,特别是如果还有其他情况会导致此问题.

One possible solution I could do is present a view controller from within the container view, that works, I would just have to set the current view controller in the container to a delegate of the root controller, so that when the top bar is tapped, it tells the view controller in the container to present the new view controller. This would not be my first option however, especially if there are other scenarios which cause this problem.

感谢您的帮助!

推荐答案

您需要为所有子视图控制器关闭 automaticallyAdjustsScrollViewInsets 并管理插入(或设置为不需要它们)) 你自己.默认情况下 automaticallyAdjustsScrollViewInsets 对所有视图控制器都是开启的(这是你想要的全屏"呈现的 VC.

You need to turn automaticallyAdjustsScrollViewInsets off for all of your child view controllers and manage the insets (or setup so they aren't required) yourself. By default automaticallyAdjustsScrollViewInsets is on for all view controllers (which is what you want for 'full screen' presented VCs.

目前,随着 VC 层次结构的变化,您会看到控制器几乎随机更新以重新组织自己以适应滚动插图.

At the moment you see controllers almost randomly updating to reorganise themselves for the scroll insets as the VC hierarchy changes.

我可能会为所有 VC(root 除外)关闭 automaticallyAdjustsScrollViewInsets 并更改您的标题视图,使其为全高或状态栏高度(应该是 length根 VC 的 topLayoutGuide 的代码>).当折叠到状态栏高度时,您的标题视图也可以更改颜色以匹配当前顶部 VC 的颜色.

I'd probably turn automaticallyAdjustsScrollViewInsets off for all VCs (apart from root) and change your header view so that it's full height or status bar height (which should be the length of the topLayoutGuide of the root VC). When collapsed to status bar height your header view could also change colour to match that of the current top VC.

这篇关于在容器视图中,导航控制器的导航栏未调整大小以包含状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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