0

I having an Application with different tabs(with different ViewControllers). I have used UITabbarController and hide the tabbar, Initially 4 tabs are there. some times I need to show only 2 or 3 different tabs. How can I hide this with buttons(with tabBar selected index).

4 tabs

enter image description here

3 other tabs

enter image description here

2 tabs

enter image description here

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • Did you create the buttons in storyboard or code? – soulshined Dec 10 '14 at 05:52
  • UItabbarController in storyBoard. – Vineesh TP Dec 10 '14 at 05:53
  • Well you can create an IBOutlet to your button and just say myButton.setHidden = NO; – soulshined Dec 10 '14 at 05:56
  • Or you can create an NSMutableArray and remove the objects or add the objects as necessary – soulshined Dec 10 '14 at 05:56
  • @soulshined: not only for button I having UItabbarController with 7 tabs(each having different Viewcontrollers). at first shows only 4 tabs. when tap on this with some condition the tabbar shows 2 or 3 tabs(i.e., different viewcontroller). – Vineesh TP Dec 10 '14 at 06:03
  • so your saying each tab bar is in it's own view controller. So VC 1 has a tab bar with 3 tabs. And VC 2 has a tab bar with 2 tabs. When you click on VC 1 tab bar with 3 tabs at the 1st bar item it presents VC 2 with a tab bar with 2 tabs correct? – soulshined Dec 10 '14 at 06:06
  • @soulshined: in some scenario only having a VC with 3 other new tabs. some times I need to show 2 existing tabs only (need to hide other tabs). – Vineesh TP Dec 10 '14 at 06:09
  • I understand, so since it's created in storyboard you can create IBOutlet to it and setHidden = YES; in your if statements. Then reverse it when necessary. – soulshined Dec 10 '14 at 06:10
  • For which controller I need to set IBOutlet ? – Vineesh TP Dec 10 '14 at 06:12

2 Answers2

0

Vineesh,

This is an example to specific tab bar items. You can allocate this anywhere in your if statements to maintain flexibility.

NSMutableArray *tabbarItems = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
[tabbarItems removeObjectAtIndex:/*Select your bar item[s] you wish to hide*/];
[self.tabBarController setViewControllers:tabbarItems];

Or you can create an IBOutlet to the bar item since your using storyboard and setHidden = YES; when necessary.

soulshined
  • 9,612
  • 5
  • 44
  • 79
0

First you need to store the instances of all the tabBarItem. The items property of UITabBar will give all the UITabbarItems currently it contains. Then you can add/remove the UITabBarItems into/ from items array according to your needs.

NSMutableArray * tabBarItems = self.tabBar.items.mutableCopy ;

If you want to remove the facebook and twitter item add the below line

[tabBarItems removeObject:self.tabbarItemFacebook];
[tabBarItems removeObject:self.tabbarItemTwitter];

self.tabBar.items = tabBarItems ;

hope that it will help you :)

coelhudo
  • 4,710
  • 7
  • 38
  • 57
Maria
  • 21
  • 4