Persist Customized Tab Bar Buttons

PDF for offline use
Sample Code:
Related Articles:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe provides one possible solution for how to save and reload a customized tab bar.

Recipe

When a Tab Bar has more options than can fit on the screen, you can allow the user to edit the order of the tabs. The changes are not automatically saved.

To save the changes made when a tab bar is edited:

  1. Implement a tab bar. See the [Specifying Customizable Tab Bar Buttons] recipe. Ensure that the Tag is set on each TabBarItem to match its position in the default ViewControllers array.
  1. Implement the FinishedCustomizingViewControllers delegate which gets called whenever the user touches Done after editing a tab bar. In this example the Tag of each item must match its default position.
FinishedCustomizingViewControllers += delegate(object sender, UITabBarCustomizeChangeEventArgs e) {
    if (e.Changed) {
        var tabOrderArray = new List<string>();
        foreach(var viewController in e.ViewControllers)  {
            var tag = viewController.TabBarItem.Tag;
            tabOrderArray.Add(tag.ToString());
        }
        NSArray arr = NSArray.FromStrings(tabOrderArray.ToArray());
        NSUserDefaults.StandardUserDefaults["tabBarOrder"] = arr;
    }
};
  1. Create a method SetCustomTabBarOrder() that retrieves the user default setting and re-creates the correct tab order. It re-sets the ViewControllers property directly with the custom order.
void SetCustomTabBarOrder()
{
    var initialViewController = this.ViewControllers;
    var tabBarOrder = NSUserDefaults.StandardUserDefaults.StringArrayForKey("tabBarOrder");
    if (tabBarOrder == null)
        Console.WriteLine ("No custom tab order, use default");
    else {
        if (tabBarOrder.Length == initialViewController.Length) {
            Console.WriteLine ("Setting order based on UserDefault");
            var newViewControllers = new List<UIViewController>();
            foreach (var tabBarNumber in tabBarOrder) {
                newViewControllers.Add(initialViewController
                    [Convert.ToInt32(tabBarNumber)]);
            }
            ViewControllers = newViewControllers.ToArray();
        }
    }
}
  1. Now call the new method SetCustomTabBarOrder() after the ViewControllers property has been set. The application will store the customized tab order after each edit and reload it when the application starts.

Additional Information

This recipe shows one option for persisting a customized tab bar, using the Tag values to help order the objects. This solution will ignore the customized values if a later version of your application has more or fewer tabs than previously.

You can write your own solution by providing a different implementation for the FinishedCustomizingViewControllers delegate.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.