17

Within my iPhone application I have a common tab bar with three tabs that is presented from several views after pressing a button. The approach I followed was the workflow of Tweetie application, described in Robert Conn post.

Note that the main controller is a navigation controller; the tab bar is placed in a view controller's NIB file of the navigation stack, and the effect of switching between tabs is handled in a delegate didSelectItem method.

@interface GameTabBarController : UIViewController<UITabBarDelegate> {
  UITabBar *tabBar;
  UITabBarItem *lastGameTabBarItem;
  UITabBarItem *previousGamesTabBarItem;
  UITabBarItem *myBetsTabBarItem;

  NSArray *viewControllers;
  UIViewController *currentViewController;
}

@implementation GameTabBarController
  ...

  - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    UIViewController *viewController = nil;

    // Get the view controller linked to the tab bar item pressed
    ...

    // Switch to the view
    [self.currentViewController.view removeFromSuperview];
    [self.view addSubview:viewController.view];
    self.currentViewController = viewController;
  }

  ...
@end

Since the views of the tab bar must be customized according to the view controller the application came from, I have made this GameTabBarController a parent class with that NIB file that have the tab bar. Then, I have created several children classes:

@interface FirstGameTabBarController : GameTabBarController {
  ...   
}

@interface SecondGameTabBarController : GameTabBarController {
  ...   
}

...

My problem is that in some of the children classes I would like to remove the third tab of the NIB file associated with parent class. But since there is no UITabBarController involved, I cannot follow typical approaches you can found on the web, i.e. removing the view controller of the tab bar item.

How can I do that? Is it possible to remove elements that has been previously added in a NIB file?

Thanks!!

UPDATE The solution was so easy... I have just to replace the tab bar items, instead of the view controllers:

NSMutableArray *items = [NSMutableArray arrayWithArray:self.tabBar.items];
[items removeObjectAtIndex:2];
[self.tabBar setItems:items];

Thanks to @Praveen S for pointing me in the right direction.

elitalon
  • 9,191
  • 10
  • 50
  • 86

4 Answers4

46

The following code has the solution:

NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tbViewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:tbViewControllers];
Kennet Jacob
  • 736
  • 7
  • 7
8

Swift 4

func removeTab(at index: Int) {
    guard let viewControllers = self.tabBarController?.viewControllers as? NSMutableArray else { return }
    viewControllers.removeObject(at: index)
    self.tabBarController?.viewControllers = (viewControllers as! [UIViewController])
}
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
mourodrigo
  • 2,232
  • 28
  • 18
  • @Yawar in this case maybe you don't have a tabbarcontroller properly configured on your viewcontroller's hierarchy. – mourodrigo Apr 03 '19 at 14:16
8

This is what it works for me for Swift 4 and 5

  1. Create a custom UITabBarController class.
  2. Assign the custom UITabBarController class to the view on storyboard.
  3. Remove the UIViewController on viewDidLoad:

    class TabViewController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.viewControllers?.remove(at: 1)
        }
    }
    
pableiros
  • 14,932
  • 12
  • 99
  • 105
2

You can hold a reference to that tab bar object in your class and perform the desired actions on it.

IBOutlet <Type> name;

Connect it via Interface builder and you can perform actions, and in your case you may be thinking of removing it from superview.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
  • You point me in the right direction, though your answer was not exactly what I was looking for. I'm updating the question to post what I finally did. – elitalon May 06 '11 at 10:17
  • 2
    You cannot remove a `UITabBarItem` from its superview directly because it is a `UIBarItem`, which inherits from `NSObject`, not `UIView`. You would have to iterate over the `UITabBar`'s subviews and figure out which one corresponds to the `UITabBarItem`, which would probably require private APIs. – devios1 Aug 11 '15 at 01:48