问题描述
我的应用程序中有一系列UIViewControllers。他们中的大多数都有导航栏,但其中一些隐藏了它。
I have a series of UIViewControllers throughout my application. Most of them have the navigation bar but some of them hide it.
问题在于,有时当你使用或不使用导航栏的视图转换到带或不带有导航栏的另一个视图时,会有一个黑盒子在转换过程中替换导航栏。这里讨论了这个问题:
The problem is that sometimes as you transition between a view with or without navbars to another view with or without navbars there is a black box that replaces the navbar during the transition. This problem was discussed here: Hiding a UINavigationController's UIToolbar during viewWillDisappear:
这个解决方案很好,它确实摆脱了黑匣子,但我真的不希望被描述为柴郡猫的消失。我尝试过使用prepareForSegue,ViewWillAppear,viewWillDisappear等无数解决方案。我能做的最好的事情就是改变黑条显示的方案。
This solution is fine and it does get rid of the black box, but I really don't want what was described as a "Cheshire Cat" disappearance. I've tried myriad solutions using prepareForSegue, ViewWillAppear, viewWillDisappear, etc. The best I can do is change the scenario in which the black bar shows up.
由此我意思是,两个导航栏状态之间有四种视图转换组合(隐藏与非隐藏):
By this I mean, there are four combinations of view transitions between the two navigation bar states (hidden vs. not-hidden):
- 隐藏 - 隐藏
- 隐藏 - 未隐藏
- 未隐藏 - 隐藏
- 未隐藏 - 未隐藏
- Hidden - Hidden
- Hidden - Not Hidden
- Not Hidden - Hidden
- Not Hidden - Not Hidden
无论我尝试过什么解决方案,至少其中一个组合会导致我的黑盒子变得丑陋。我遇到的问题是,当从导航控制器的视图堆栈中弹出一个视图时,我无法找到任何可以获取对源视图控制器和目标视图控制器的引用的地方。
No matter what solution I've tried, at least one of those combinations results in my black box rearing its ugly head. The problem I have is that I've been unable to find anywhere that I can get a reference to the source view controller and the destination view controller when popping a view off of the navigation controller's view stack.
如果我能在同一事件中获得两个引用,我可以简单地确定组合是什么,并像我准备segue一样适当地处理行为。
If I could get both references in the same event, I could simply determine what the combination is and handle the behavior appropriately like I would in prepare for segue.
现在,我知道这是不可能的是一个合理的(甚至是可能的)答案,但我不会接受仅作为解决方案。如果确实不可能,我会想出一个合理的选择。例如,我可以手动处理所有视图控制器弹出(包括默认的后退按钮),因此可以从导航控制器的堆栈中获取即将到来的控制器。
Now, I know that "it's not possible" is a reasonable (and even a probable) answer, but I won't accept that as a solution alone. If it is indeed not possible, I'd like thoughts on a reasonable alternative. For example, I could handle all view controller popping manually (including the default back button) and thus could get the "upcoming controller" from the navigation controller's stack.
我会只是喜欢使用内置API的解决方案,或者至少是我的控制器不必知道自己的导航栏状态的解决方案。
I would just prefer a solution using built in APIs or at least a solution where my controllers didn't have to be aware of their own navigation bar states.
非常感谢,
Patrick
Thanks a lot,Patrick
推荐答案
我认为 UINavigationControllerDelegate
就是你的意思后。它声明了两种方法:
I think UINavigationControllerDelegate
is what you're after. It declares two methods:
-
-navigationController:willShowViewController:animated:
-navigationController:didShowViewController:animated:
您需要做的就是将自己设置为父导航控制器的委托,并实现这些方法以通知传入的视图控制器。
All you need to do is set yourself as the delegate of the parent navigation controller and implement these methods to be notified of incoming view controllers.
话虽如此,我从来不需要求助于隐藏和显示导航栏。严格来说,查看导航栏始终可见的控制器从不触摸导航栏的可见性。当我进入需要隐藏的视图控制器时,该视图控制器负责隐藏并将其恢复到先前状态,然后消失。事实证明,遵循这些标准对我来说是可靠的。
Having said that, I've never needed to resort to this for hiding and showing navigation bars. Strictly speaking, view controllers where the navigation bar will always be visible never touch the navigation bar's visibility. When I'm moving into a view controller where it needs to hide, that view controller is responsible for hiding and setting it back to its prior state before disappearing. Following these standards has proven reliable for me.
这篇关于同时引用源视图控制器和目标视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!