Show and Hide the Master View Button

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

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to add a button and popover to a UISplitViewController to display the master view in portrait orientation.

Recipe

The UISplitViewController can help display a button & popover for the master view controller when it is hidden. This recipe shows how to add a toolbar to the detail view and implement the popover button.

To show and hide the master view:

  1. Start with an existing implementation of UISplitViewController, such as the [Using a Split View to Show Two Controllers](/recipes/ios/content_controls/split_view/use_split_view_to_show_two_controllers) recipe.
  1. Add a toolbar the DetailViewController so that there is a host for the popover button (when it is required):
// add a toolbar to host the master view popover (when it is required, in portrait)
toolbar = new UIToolbar(new CGRect(0, 0, View.Frame.Width, 30));
toolbar.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
View.AddSubview(toolbar);
  1. Implement two methods on the DetailViewController that will be used to show and hide the button.
/// <summary>
/// Shows the button that allows access to the master view popover
/// </summary>
public void AddContentsButton (UIBarButtonItem button)
{
    button.Title = "Contents";
    toolbar.SetItems (new UIBarButtonItem[] { button }, false );
}

/// <summary>
/// Hides the button that allows access to the master view popover
/// </summary>
public void RemoveContentsButton ()
{
    toolbar.SetItems (new UIBarButtonItem[0], false);
}
  1. Implement a UISplitViewControllerDelegate the SplitViewController to control when the button is shown or hidden:
public class SplitViewDelegate : UISplitViewControllerDelegate {
    public override bool ShouldHideViewController (UISplitViewController svc, UIViewController viewController, UIInterfaceOrientation inOrientation)
    {
        return inOrientation == UIInterfaceOrientation.Portrait
            || inOrientation == UIInterfaceOrientation.PortraitUpsideDown;
    }

    public override void WillHideViewController (UISplitViewController svc, UIViewController aViewController, UIBarButtonItem barButtonItem, UIPopoverController pc)
    {
        DetailViewController detailView = svc.ViewControllers[1] as DetailViewController;
        detailView.AddContentsButton (barButtonItem);
    }

    public override void WillShowViewController (UISplitViewController svc, UIViewController aViewController, UIBarButtonItem button)
    {
        DetailViewController detailView = svc.ViewControllers[1] as DetailViewController;
        detailView.RemoveContentsButton ();
    }
}
  1. Set the Delegate in the SplitViewController's constructor:
Delegate = new SplitViewDelegate();

Additional Information

The example above does not include any interaction between the views. See the Communicating Between Detail and Master Controllers recipe.

You can change the behavior of the SplitViewController to always show the master view (or always show the button & popover) by returning false or true respectively from ShouldHideViewController.

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.