2011-02-13 110 views
13

我正在嘗試執行以下操作。將TabBar隱藏在導航堆棧中並在彈出導航堆棧時將其拿回來

我有一個帶有2個選項卡的標籤欄控制器。這兩個選項卡都是導航控制器,每個選項卡上都有一個表格視圖。

現在,當我在第一個選項卡中選擇一個表格單元格時,我正在推動另一個選項卡控制器,因此我想要隱藏父級tabbarcontroller的選項卡欄,並且當我單擊導航上的後退按鈕時酒吧我想再次看到父母標籤欄,因爲我在我的父母標籤欄視圖中。

我試過hidesbottombarwhenhen,它隱藏了父標籤欄控制器標籤欄,但是當我點擊它並沒有把它帶回來。

回答

38

好吧,所以最後我得到了我的答案,這是我想要做的。

self.hidesBottomBarWhenPushed = YES; 
[self.navigationController pushViewController:aViewController animated:YES]; 
self.hidesBottomBarWhenPushed=NO; 

所以基本上hidesBottomBarWhenPushed = YES,然後把你的視圖控制器然後hidesBottomBarWhenPushed = NO;這就像一個魅力。

由於eddy和他的崗位here

+0

我正面臨着同樣的問題,謝謝你! – Mateus 2012-11-18 02:22:01

1

正如Apple文檔所述,您無法在NavigationController上推送UITabBarController實例。有一個很好的理由:如果您在標籤欄中選擇了另一個項目,如何從推送的標籤欄控制器返回?

簡單的答案是:不這樣做,它會混淆你的用戶。您可以嘗試將第一個視圖控制器交換爲另一個視圖控制器,該視圖控制器可能是一個標籤欄控制器,但不要使用推式範例:使用顯式按鈕來代替第二個視圖控制器,最好使用一個視覺過渡。

你可以看一下setAnimationTransition:forView:cache:文檔的UIView類要懂得交換,比方說,一個標籤欄控制器,另:

  1. 開始動畫塊。
  2. 設置容器視圖上的轉換。
  3. 從容器視圖中刪除子視圖。
  4. 將新的子視圖添加到容器視圖。
  5. 提交動畫塊。

在這種情況下,容器視圖將是應用程序的窗口。

+0

當我推着的UITabBarController到導航控制器,讓我們說的UITabBarController有三個選項卡,所以我會假設,第一個選項卡將有一個後退按鈕以轉到父視圖。如果用戶在任何其他選項卡上,我明白他不能回到父視圖,我想我可以。所以我想知道我應該怎麼做 – Yogesh 2011-02-14 03:21:43

1

您也可以使用屬性檢查器中隱藏它時選擇的TabBar控制器

enter image description here

5

接受的答案有一個問題給我。

我的應用程序的導航深度爲三個UIViewController。

  • FirsViewController顯示的是UITabBar。(正確)
  • FirsViewController推送SecondViewController,並且SecondViewController不顯示UITabBar。 (正確)
  • SecondViewController推送ThirdViewController,ThirdViewController顯示的是UITabBar。 (不正確)
  • ThirdViewController彈出到SecondViewController,SecondViewController顯示的是UITabBar。 (不正確)
  • SecondViewController彈出到FirstViewController,FirstViewController顯示的是UITabBar。 (正確)

對我來說是設置UINavigationControllerDelegate的代表解決方案

迅速:

self.navigationController?.delegate = self 

的Objective-C:

self.navigationController.delegate = self; 

,然後實現以下的委託方法

斯威夫特:

fun navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { 

    if fromVC.isKindOfClass(FirstViewController) && toVC.isKindOfClass(SecondViewController) { 

     self.hidesBottomBarWhenPushed = true; 

    } 
    else if fromVC.isKindOfClass(SecondViewController) && toVC.isKindOfClass(FirstViewController) { 

     self.hidesBottomBarWhenPushed = false; 

    } 

    return nil 

} 

的Objective-C:

-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController 
           animationControllerForOperation:(UINavigationControllerOperation)operation 
               fromViewController:(UIViewController*)fromVC 
               toViewController:(UIViewController*)toVC 
{ 

    if ([fromVC isKindOfClass:[FirstViewController class]] && [fromVC isKindOfClass:[SecondViewController class]]) { 

     self.hidesBottomBarWhenPushed = true; 

    } 
    else if ([fromVC isKindOfClass:[SecondViewController class]] && [fromVC isKindOfClass:[FirstViewController class]]) { 

     self.hidesBottomBarWhenPushed = false; 

    } 

    return nil; 

} 

希望它幫助。

0

在你FirstViewController使用

self.hidesBottomBarWhenPushed = true 

在SecondViewController使用

override func willMoveToParentViewController(parent: UIViewController?) { 
     if parent == nil { 
      var viewControllers = self.navigationController!.viewControllers 
      if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstViewController.self)) { 
       (viewControllers[viewControllers.count - 2] as! FirstViewController).hidesBottomBarWhenPushed = false 
      } 
     } 
    }