簡體   English   中英

將控制器添加到UITabBarController,而標簽欄中沒有出現新項目

[英]Add controller to UITabBarController without new item appearing in the tab bar

我有一個UITabBarController作為其主控制器的應用程序。

當用戶點擊一個按鈕(不在標簽欄,只是其他一些按鈕)時,我想在我的UITabBarController中添加新的UIViewController並顯示它,但我不希望新的UITabBarItem出現在標簽欄中。 如何實現這樣的行為?

我試圖將tabBarController.selectedViewController屬性設置為不在tabBarController.viewControllers數組中的視圖控制器,但沒有任何反應。 如果我將視圖控制器添加到tabBarController.viewControllers數組,則新項目將自動顯示在選項卡欄中。

更新

感謝Levi,我擴展了我的標簽欄控制器來處理.viewControllers中沒有的控制器。

@interface MainTabBarController : UITabBarController

/** 
 * By setting this property, tab bar controller will display
 * given controller as it was added to the viewControllers and activated
 * but icon will not appear in the tab bar.
 */
@property (strong, nonatomic) UIViewController *foreignController;

@end


#import "MainTabBarController.h"

@implementation MainTabBarController

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
  self.foreignController = nil;
}

- (void)setForeignController:(UIViewController *)foreignController
{
  if (foreignController) {
    CGFloat reducedHeight = foreignController.view.frame.size.height - self.tabBar.frame.size.height;
    foreignController.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, reducedHeight);

    [self addChildViewController:foreignController];
    [self.view addSubview:foreignController.view];
  } else {
    [_foreignController.view removeFromSuperview];
    [_foreignController removeFromParentViewController];
  }

  _foreignController = foreignController;
}

@end

代碼將正確設置“外部”控制器的視圖大小,並在用戶在選項卡欄中選擇項目時將其刪除。

您可以按下它(如果您有導航控制器),也可以將其視圖添加到可見的View Controller視圖中,並將其添加為子視圖控制器。

您可以使用以下內容顯示新視圖控制器:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;

或者,如果你的一個UIViewControllersUINavigationController ,你可以:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM