0

How do I hide an individual tab bar button ?

I've searched and not found anything, only the full bar.

I've made some progress but still having problems, this code is in my app delegate with an outlet to the tab bar, I'm calling it within viewDidLoad of the first view shown in tab bar.

-(void)hideTabButton {  
NSMutableArray *aItems = [[rootTabBar items] mutableCopy];
for (UITabBarItem *tabButton in aItems) {
    if ([tabButton.title isEqualToString:@"First"]) {           
        [aItems removeObject:tabButton];
        break;
    }
}
[rootTabBar setItems:aItems animated:YES];
[aItems release];   
}

But this gives me an error, it does seem to be possible otherwise why have setItems.

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Directly modifying a tab bar managed by a tab bar controller is 
not allowed.'
Call stack at first throw:

Heres my full code, think I'm close. My Sample project

Jules
  • 7,568
  • 14
  • 102
  • 186

2 Answers2

3

You would need to use setItems:animated: to do this. Create an array of the buttons you want to keep on the UITabBar and pass it to this instance method:

[myTabBar setItems:itemsToKeep animated:TRUE];

Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBar_Class/Reference/Reference.html

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • Can I load the items into the array from the buttons I've created in IB ? – Jules Nov 16 '10 at 16:39
  • Yes, create an NSMutableArray with `myTabBar.items`. Use the `removeObject:` method on the array to remove a specific button by name. Make sure the button(s) you want to remove are connected to the code. – Evan Mulawski Nov 16 '10 at 16:43
  • Do you want to remove the view associated with the button, or do you just want to hide one button? – Evan Mulawski Nov 17 '10 at 12:55
  • It is definitely possible, otherwise there wouldn't be a `setItems` method. Create a method in you tab bar controller class that performs the button hide (your code above). Call that method in the location above. – Evan Mulawski Nov 17 '10 at 15:27
  • Problem solved - create an outlet to the UITabBar directly. Then use `[myTabBar setItems...]` instead of `[tabBarController.tabBar setItems...]`. – Evan Mulawski Nov 17 '10 at 17:19
  • I am not getting any errors, just a black screen - with or without calling the `hideTabButton` method. – Evan Mulawski Nov 17 '10 at 17:43
  • I assume if it worked you updated my project files, can you upload them for me ? I can do a diff comparison on them and add them to my main project :) – Jules Nov 17 '10 at 21:01
  • If you don't call `hideTabButton`, do you get a black screen? – Evan Mulawski Nov 17 '10 at 21:01
  • I uploaded your modified project. See if it works for you: http://skydrive.provanix.com/stackoverflow/tabbar2a_modified.zip. You may need to change the Base SDK back to 4.1. – Evan Mulawski Nov 17 '10 at 21:41
  • I get a black screen even if I don't set the tab bar items. – Evan Mulawski Nov 17 '10 at 22:05
  • OK, I'm not calling the app delgate hideTabBarButton, heres the code which produces the error http://www.kidsmaskfactory.com/code/tabbar2c.zip I think my problem might be that I'm calling hideTabBarButton in my first view used by the tab bar, if I could call it in app delegate before I think this might be the answer. Can you have a look ? – Jules Nov 18 '10 at 09:34
1

In an older thread Tab bar Controller raising NSInternalInconsistencyException I found a warning to use an outlet to the tabbar drectly. I had this problem before and got the Error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

But this error was only on devices with iOS 3.1.x, not with iOS 4.x.

In order to find a way to run my program on older devices with 3.1.x I first removed the outlet and all references to it, even in IB.

Because I need to disable some tabs I didn't find another way to do that. So I've reinstalled the outlet and all the references to it. Now it is the same code as before and it works!

So it is worth a try to do the same.

Community
  • 1
  • 1