简体   繁体   中英

iPhone, how I hide a tab bar button?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM