views:

2932

answers:

9

In my application (based on the Tab bar application XCode template) I use a UITabBarController to display a list of different sections of the application that the user can access.

By default, the UITabBarController displays a 'More' button in the tab bar when there are more than 5 items. Also, it allows the user to select the items that he want to be visible in the tab bar.

Currently I can't implement saving and loading the state of the tab bar controller, so I want to disable the 'Edit' button.

Is there any way to disable/hide the 'Edit' bar button that appears on the 'More' navigation controller of UITabBarController?

I tried:

tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil;

and

tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem.enabled = NO;

but they don't seem to work.

A: 

tabBarController .customizableViewControllers = nil;

m4rkk
Thank you, this doesn't hide or disable the 'Edit' button, but it displays an empty customization dialog. It's better than nothing, but it doesn't do exactly what I want.
Panagiotis Korros
+5  A: 

customizableViewControllers is an array; set it to the empty array to disable all editing.

tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
Ian Terrell
Perfect. Thanks.
pluckyglen
its removing that array..But how to hide that button..?
Sijo
It does indeed remove the edit button. Obviously the UITabBarController checks if there are any customisable buttons and only displays the edit button in that case. Alternatively, you could just set the property to nil instead of setting it to an empty array.
Johnus
+1  A: 

@m4rkk & @lan terrell that code does not work.

i wasnt able to get it so i just disable the navigation bar alltogether.

tabBarController.moreNavigationController.navigationBar.hidden = YES;
odyth
any idea how to remove edit button only ?
Sijo
+11  A: 

Become a delegate of moreViewController (it is a UINavigationController) and add this:

- (void)navigationController:(UINavigationController *)navigationController
        willShowViewController:(UIViewController *)viewController
        animated:(BOOL)animated {

    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    /* We don't need Edit button in More screen. */
    morenavitem.rightBarButtonItem = nil;
}

Now it won't appear. The key thing to consider is that Edit button appears not after controller creation, but before displaying the view, and we should sit silently till that moment and then, when the controller is going to display the screen, we will knock the button out so that it won't have a chance to create it again. :)

Aleks N.
Nicely done! Q: Is it possible for something else (internal to iPhone OS) to already be a delegate to the moreViewController? Not that you'd know for sure. I'm just wondering out loud here ... playing what if.
Joe D'Andrea
superb..this is what i was looking for...thanks for this great help..
Sijo
+1  A: 

This can be achieved like such. It is not the most elegant solution, but It Works™.

// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self performSelector:@selector(removeEdit) withObject:nil afterDelay:.0001];
}
- (void)removeEdit
{
    tabBarController.moreNavigationController.navigationBar.topItem.rightBarButtonItem = nil; 
}
Logan Cautrell
this is working for me working
hib
didnt worked for me
Sijo
Not very elegant...
Pierre Valade
A: 

Thanks. Big help. That was driving me crazy.

Michael Bordelon
A: 

tabBarController .customizableViewControllers = nil;

AND

tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];

have both worked perfectly before. But on os4.0, the Edit button is still displayed. Any solutions?

adranale
A: 

I don't know about OS4, but it matters if you put the code in viewDidLoad vs viewWillAppear.

Ie, this will work.

  • (void)viewWillAppear:(BOOL)animated { self.customizableViewControllers = nil; }
Rob
Huh ... and now I'm upgraded to iOS4, and surprise, surprise - the Edit button has come back.
Rob
Huh ... and now I'm upgraded to iOS4, and surprise, surprise - the Edit button has come back.But eisornWolfs answer does work. I found that the easiest way to become a delegate was to put that code in my UITabbarController subclass: - (void)viewWillAppear:(BOOL)animated{ self.moreNavigationController.delegate = self;}and then implement the method (- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated) right in the same class file.
Rob
A: 

I was able to get this working with the following code. I created a CustomTabViewController and then modified my Tab Bar Controller's Class Identity in Interface Builder to use this custom class. Here is the code that it uses (.h and .m file contents). The key is setting the property to nil, which causes the Edit button to not be displayed. For details see: http://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/customizableViewControllers "If the array is empty or the value of this property is nil, the tab bar does not allow any items to be rearranged."

#import <UIKit/UIKit.h>

@interface CustomTabBarController : UITabBarController {

}
@end

#import "CustomTabBarController.h"


@implementation CustomTabBarController

- (void)viewDidLoad
{
    self.customizableViewControllers = nil;
    [super viewDidLoad];
}   

@end
Andre Oporto