objective-cios7swiftuitabview

Hide a tab bar item but still display the controller


I will accept answers in both swift and objective-c as the translation is fairly easy.

I want to display a tab bar with a splash screen, but I don't want that splash screen to be in the tab bar items for selection.

My setup now (below) shows the landing screen as the first displayed controller when the tab bar displays. However, I want that tab bar item hidden. Only the other three tabs should be selectable by a user. How do I do this?

//Create and add landing view
        navigation = UINavigationController()
        landingView = WGMLandingViewController(nibName: XIBFiles.LANDINGVIEW, bundle: nil)
        navigation.pushViewController(landingView, animated: false)
        navigation.title = "Landing View"
        controllers.append(navigation)

        //Create and add library view
        navigation = UINavigationController()
        libraryView = WGMLibraryViewController(nibName: XIBFiles.LIBRARYVIEW, bundle: nil)
        navigation.pushViewController(libraryView, animated: false)
        navigation.title = "Learn More"
        controllers.append(navigation)

        //Create and add pad view
        navigation = UINavigationController()
        orderPadView = WGMOrderPadViewController(nibName: XIBFiles.ORDERPADVIEW, bundle: nil)
        navigation.pushViewController(orderPadView, animated: false)
        navigation.title = "Order Pad"
        controllers.append(navigation)

        //Create and add lookup view
        navigation = UINavigationController()
        partLookupView = WGMLookupViewController(nibName: XIBFiles.LOOKUPVIEW, bundle: nil)
        navigation.pushViewController(lookupView, animated: false)
        navigation.title = "Lookup"
        controllers.append(navigation)

        //Set up controller list
        self.setViewControllers(controllers, animated: false)

Solution

  • Apple's existing APIs don't allow you to do this, but we it shouldn't too hard to sub-class UITabBarController and get it to do what you want.

    Ok.. my original answer won't work these days.. or I've gone senile and it never worked and I did something else. *cough*

    So anyway, you'll have to roll your own tab bar controller. It's not so hard (just time consuming) now that we have containment view controllers and you can still use the UITabBar.

    1. Create a UIViewController (your tab bar controller) and stick a UITabBar and a UIView in it.
    2. Create an outlet for the view (this is where your view controllers will go) and tab bar
    3. Configure the UITabBar however you'd like it.
    4. Set the delegate of the UITabBar to be your view controller and implement the didSelectItem method.
    5. Create a method to load your splash screen view controller and stick in your viewDidLoad or where you want it.

    Something like

     - (void)loadSplashScreen  {
        // load in the child view controller
        UIViewController *splashScreenController = ...;
        [self loadChildViewController:splashScreenController];
    
        // make sure nothing in the tab bar is selected
        self.tabBar.selectedItem = nil;
    }
    

    Then also add code to load the appropriate view controller whenever the various tabs are selected

     - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        // logic to figure out which view controller you want based on `item`
        // ...
    
        UIViewController = ...;
        [self loadChildViewController:viewController];
    }
    
    - (void)loadChildViewController:(UIViewController *)viewController {
        [self removeCurrentTabController]; // remove the existing one, if any using whatever memory management techniques you want to put in place.
    
        [self addChildViewController:viewController];
        [self.tabView addSubview:viewController.view];  // where self.tabView is your outlet from step 2.
    }