Tuesday, February 1, 2011

Hide Right Bar(EDIT) Button of MoreNavigationController of a UITabBarController using Monotouch

To remove the Edit button from the "More" Navigation controller in a UITabBarController as shown.



First assign a delegate class to the instance of the MoreNavigatioController in the AppDelegate class

tabBarController.MoreNavigationController.Delegate = new MoreNavigationControllerDelegate();


public partial class AppDelegate : UIApplicationDelegate
{
  TabBarController tabBarController;
  // This method is invoked when the application has loaded its UI and its ready to run
  public override bool FinishedLaunching (UIApplication app, NSDictionary options)
  {
     // Create the tabs
      tabBarController = new TabBarController ();
      tabBarController.MoreNavigationController.Delegate = new MoreNavigationControllerDelegate();
   
     // Create the main window and add the navigation controller as a subview
      window = new UIWindow (UIScreen.MainScreen.Bounds);
      window.AddSubview(tabBarController.View);
      window.MakeKeyAndVisible ();
      return true;
  }

  // This method is required in iPhoneOS 3.0
  public override void OnActivated (UIApplication application)
  {
  }
}
 


  • Add a new class "MoreNavigationControllerDelegate" to the project.
  • This class should implement "UINavigationControllerDelegate".
  • Add an override method  "WillShowViewController" to this class - This method is called whenever its time to load the MoreNavigation view controller.
  • Set the RightBarButtonItem to null in this method.
The following code in "WillShowViewController" method will hide "Edit" button in the "More" controller of UITabBarController.

public class MoreNavigationControllerDelegate : UINavigationControllerDelegate
 {
  public MoreNavigationControllerDelegate () : base()
  {
  }
  
  public override void WillShowViewController (UINavigationController navigationController, UIViewController viewController, bool animated)
  {
   if(navigationController.NavigationBar.TopItem.Title == "More")
    navigationController.NavigationBar.TopItem.RightBarButtonItem = null;
  }
 }



Note- I have the condition if(navigationController.NavigationBar.TopItem.Title == "More"), since I still want to show the right bar button when I select the "Motivation", "Tips" and "Glossary" view controller as shown below.