id | title | brief | article | sdk | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
36244106-F6EA-8F5B-D2F4-12B4069C9723 |
Show and Hide the Master View Button |
This recipe shows how to add a button and popover to a UISplitViewController to display the master view in portrait orientation. |
|
|
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:
- 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.
- 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();
toolbar.TranslatesAutoresizingMaskIntoConstraints = false;
var tbConstraints = new[]
{
toolbar.LeadingAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.LeadingAnchor),
toolbar.TrailingAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.TrailingAnchor),
toolbar.TopAnchor.ConstraintEqualTo(this.View.SafeAreaLayoutGuide.TopAnchor, 0.0f),
toolbar.HeightAnchor.ConstraintEqualTo(toolbar.IntrinsicContentSize.Height)
};
View.AddSubview(toolbar);
NSLayoutConstraint.ActivateConstraints(tbConstraints);
- 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);
}
- Implement a
UISplitViewControllerDelegate
theSplitViewController
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 ();
}
}
- Set the
Delegate
in theSplitViewController's
constructor:
Delegate = new SplitViewDelegate();
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
.