当前视图控制器当前tabBarController与NavigationController [英] Present View Controller Over current tabBarController with NavigationController

查看:220
本文介绍了当前视图控制器当前tabBarController与NavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在展示或解雇VC时,我不想继续隐藏和显示tabBar,因为它会造成糟糕的用户体验。相反,我希望在标签栏上直接显示下一个VC,这样当我通过从左向右缓慢拖动来关闭nextVC时,我可以看到隐藏在视图后面的tabBar(如下图所示)

When presenting or dismissing VC, I do not want to keep hiding and showing tabBar because it creates a poor user experience. Instead, I want present the next VC straight over the tab bar such that when I dismiss the nextVC by dragging slowly from left to right, I can see the tabBar hidden behind the view (As shown in image below)

注意,我的应用程序有两个选项卡,其中包含两个与之关联的VC(VCA,VCB)。两个VC都嵌入了导航栏。 VCA分段为VCA1,VCB分段为VCB1。目前,在VCA和VCB内部,当调用viewWillappear(下面的代码)时,我调用以下函数来进行一些隐藏和取消隐藏。

Note, my app has two tabs with two VCs(VCA,VCB) associated to it. Both VC also have navigation bar embedded. VCA segues to VCA1 and VCB segues to VCB1. At the moment, inside VCA and VCB I am calling the following function to segue with some hiding and unhiding done when viewWillappear (Code below).

self.navigationController?.showViewController(vc, sender: self)

  // Inside ViewWillAppear Only reappear the tab bar if we successfully enter Discover VC (To prevent drag back half way causing tab bar to cause comment entry to be floating). This code check if we have successfully enters DiscoverVC
    if let tc = transitionCoordinator() {
        if tc.initiallyInteractive() == true {
            tc.notifyWhenInteractionEndsUsingBlock({(context: UIViewControllerTransitionCoordinatorContext) -> Void in
                if context.isCancelled() {
                    // do nothing!
                }
                else {
                    // not cancelled, do it
                    self.tabbarController.tabBar.hidden = false
                }
            })
        } else {
            // not interactive, do it
            self.tabbarController.tabBar.hidden = false
        }
    } else {
        // not interactive, do it
        self.tabbarController.tabBar.hidden = false
    }

----------来自GOKUL的工作解决方案-----------

----------Working solution from GOKUL-----------

Gokul的答案接近现场。我已经玩了他的解决方案并提出了以下改进,以消除冗余VC的需要,并且还消除了在tabVC出现之前短暂显示的初始VC。但是如果没有Gokul,我永远不会想到这个!!

Gokul's answer is close to spot on. I have played with his solution and came up with the following improvement to eliminate the need to have a redundant VC and also eliminate the initial VC being shown for a brief second before tabVC appears. But without Gokul, I would never ever come up with this!!

此外,Gokul的方法会为我创建一个错误,因为即使我确实有一个初始的正常显示tabVC之前的VC作为LoginVC。如果用户需要登录,则此loginVC仅为rootVC。因此,在大多数情况下,通过将rootVC设置为tabVC,将永远不会注册navVC。

Additionally, Gokul's method would create a bug for me because even though I do have a initial "normal" VC as LoginVC before tabVC is shown. This loginVC is ONLY the rootVC if the user needs to login. So by setting the rootVC to tabVC in most cases, the navVC will never be registered.

解决方案是将导航控制器和tabBar控制器嵌入到一个VC中。但它仅在navVC位于TabBarVC之前才有效。我不知道为什么但是允许我使用navVC-> tabVC-> VC1 / VC2的唯一方法是先用VCV嵌入VC1而不是再次点击VC1嵌入tabVC(它不允许我插入一个之前) tabVC和我还必须在嵌入NavVC后再次单击VC1)。

The solution is to embed navigation controller and tabBar controller to one VC. But it ONLY works if the navVC is before the TabBarVC. I am not sure why but the only way that allowed me to have navVC-> tabVC-> VC1/VC2 is to embed VC1 with a navVC first than click on VC1 again to embed tabVC (It wouldn't allow me to insert one before tabVC and I also had to click the VC1 again after embedding the NavVC).

推荐答案

您的要求我们需要在您给定的视图层次结构中进行一些小的更改

For your requirement we need to make some small changes in your given view hierarchy


  • 让我一步一步解释,

  • Let me explain step by step,


  1. 为了满足您的要求,我们必须添加 UIViewController (假设 InitialVC )嵌入了 UINavigationController 并将其设为初始viewcontroller

  1. To meet your requirement we have to add a UIViewController(let's say InitialVC) embedded with a UINavigationController and make it as initial viewcontroller.

然后用2个VC添加 UITabbarController VCA,VCB )// 重要提示: 没有任何 navigationcontroller 嵌入。

Then add a UITabbarController with 2 VC (VCA,VCB) // IMPORTANT: Without any navigationcontroller embedded.

添加 segue InitalVC TabbarController 之间带有唯一标识符(例如:初始)

Add a segue between InitalVC and TabbarController with an unique identifier(ex: Initial)

viewWillAppear InitalVC 执行如下所示的segue( InitialVC 对我们的设计是不必要的,我们使用它来桥接 navigationController tabbarController )。

In viewWillAppear of InitalVC perform segue as below (InitialVC is unnecessary to our design we are using this just to bridge navigationController and tabbarController).

    self.performSegueWithIdentifier("Initial", sender: nil)


  • TabbarController中 class隐藏您的后退按钮,这可确保无法访问 InitialVC

  • In TabbarControllerclass hide your back button, this ensures that InitialVC is unreachable.

        override func viewDidLoad() {
           super.viewDidLoad()
           self.navigationItem.hidesBackButton = true
        }
    


  • 现在添加 segue VCA VCA1 之间的按钮,即它构建和运行,您将看到VCA1通过VCA的标签栏呈现。

  • Now add a segue from a button between VCA and VCA1, thats it build and run you will see VCA1 presenting over VCA's tabbar.


  • 我们更改了什么?

  • What we have changed?


    1. 而不是添加 UINavigationController UITabbarController 里面我们反过来了。我们无法在导航中直接添加Tabbar,以便我们在它们之间使用 InitialVC

    1. Instead of adding UINavigationController inside UITabbarController we have done vice versa. We can't directly add Tabbar inside navigation to do that we are using InitialVC between them.


  • 结果:

    这篇关于当前视图控制器当前tabBarController与NavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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