user9547627
user9547627

Reputation:

How to hide a UITabBar item?

I would like to create a switch button to hide a UITabBar item. Can you explain me how can I create this?

My switch button must hide the automatic item when is on and show it when is off.

Upvotes: 0

Views: 99

Answers (2)

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

Updated Answer

After Some comments by users @switchCTRL , @Ashley Mills to add diffrent ways to hide and show. you can do subclass of UITabBarController according to @switchCTRL

And use like that if you create your TabbarController With Code

  CustomTabbarViewController *tabbarController  = [[CustomTabbarViewController alloc]init];
    tabbarController.viewControllers = [NSArray arrayWithObjects:ViewController1,ViewController2,ViewController3,ViewController4,ViewController5 ,nil];

if you create in Stroybord just make sure that your TabbarContoller'Supercalss is CustomTabbarViewController

CustomTabbarViewController:


CustomTabbarViewController.h

#import <UIKit/UIKit.h>

@interface CustomTabbarViewController : UITabBarController

-(void)hideDynamicTabbarItem;
-(void)showDynamicTabbarItem;

@end

CustomTabbarViewController.m

#import "CustomTabbarViewController.h"

@interface CustomTabbarViewController (){
  UIViewController *dynamicController;
}

@end

@implementation CustomTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(void)hideDynamicTabbarItem{
    if (self.viewControllers.count > 2) {
        NSMutableArray *allViewControllers = (NSMutableArray*)self.viewControllers;
        dynamicController = allViewControllers[2];
        [allViewControllers removeObjectAtIndex:2];
        self.viewControllers = allViewControllers;
    }
}
-(void)showDynamicTabbarItem{
  
        NSMutableArray *allViewControllers = (NSMutableArray*)self.viewControllers;
        [allViewControllers insertObject:dynamicController atIndex:2];
        self.viewControllers = allViewControllers;
    
}


@end

To Hide:

if ([self.tabBarController isKindOfClass:[CustomTabbarViewController class]]) {
    
    [(CustomTabbarViewController*)self.tabBarController hideDynamicTabbarItem];
}

To Show:

   if ([self.tabBarController isKindOfClass:[CustomTabbarViewController class]]) {
        
        [(CustomTabbarViewController*)self.tabBarController showDynamicTabbarItem];
    }

Another Simple Solution but not preferred:

In your AppDeleagte.h file add dynamicController

@interface AppDelegate : UIResponder <UIApplicationDelegate>
   @property (strong, nonatomic) UIViewController *dynamicController;
@end

Then when you Hide :

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                      
    NSMutableArray *viewControllers = (NSMutableArray *) 
    self.tabBarController.viewControllers;
    
    appDelegate.dynamicController = viewControllers[2]; // to add later
    [viewControllers removeObject:APPDELEGATE.dynamicController];
    [self.tabBarController setViewControllers:viewControllers];

And when you show :

  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
       [viewControllers insertObject:appDelegate.dynamicController atIndex:2];
        [self.tabBarController setViewControllers:viewControllers];

Upvotes: -1

switchCTRL
switchCTRL

Reputation: 188

You hav to remove your controller (not the tab directly) from the array of managed controllers by the UITabBarController and re-add it later.

To remove the item:

NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers removeObjectAtIndex:2];
[self.tabBarController setViewControllers:viewControllers];

Hold a strong reference to your removed controller and re-add it later:

NSMutableArray *viewControllers = (NSMutableArray *) self.tabBarController.viewControllers;
[viewControllers insertObject:yourRemovedController atIndex:2];
[self.tabBarController setViewControllers:viewControllers];

Upvotes: 3

Related Questions