Detect when both primary and secondary views are displayed by UISplitViewController

Apparently I should be able to use NavigationController.SplitViewController.Collapsed to determine if both the first and secondary (master/detail) views are displayed on the iPhone in landscape mode. But this property always returns "cannot evaluate" wherever I try it in the view controller class. Any suggestions?

Posts

  • BruceHamiltonBruceHamilton USMember ✭✭

    I was way off.

    This is the way to do it.

    In my ViewDidLoad, I state my preferences for iPad and iPhone in regards to the SplitViewController.

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
    {
        SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.Automatic;
    
    }
    else if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible;
    }
    
    

    For a particular view controller I have, a 'Settings' view controller, it makes sense to pop it when the iPhone is rotated to landscape when both primary (master) and secondary (detail) are displayed on iOS 8 and greater. This is because that Settings view controller is displayed via a RightBarButtonItem from the MasterViewController - and I don't want to risk the Settings from being displayed again (in the primary and secondary at the same time).

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone &&
        UIDevice.CurrentDevice.Orientation != UIDeviceOrientation.Portrait)
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
        {
            NavigationController.PopViewController(true);
        }
    }
    

    This is just one of the several possibilities with my app that I have to work through.

    Another thing to note is that the iOS Master/Detail template Visual Studio no longer has the code in AppDelegate that makes the master view controller appear first for the iPhone. Here's that code to add to FinishedLaunching:

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
    
        // Override point for customization after application launch.
    
        var splitViewController = (UISplitViewController)Window.RootViewController;
        var navigationController = (UINavigationController)splitViewController.ViewControllers[1];
        navigationController.TopViewController.NavigationItem.LeftBarButtonItem = splitViewController.DisplayModeButtonItem;
        splitViewController.WeakDelegate = this;
    
        return true;
    }
    

    And you need to add this method and attribute to AppDelegate.

    [Export("splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:")]
    public bool CollapseSecondViewController(UISplitViewController splitViewController, UIViewController secondaryViewController, UIViewController primaryViewController)
    {
    if (secondaryViewController.GetType() == typeof(UINavigationController) &&
        ((UINavigationController)secondaryViewController).TopViewController.GetType() == typeof(DetailViewController) &&
        ((DetailViewController)((UINavigationController)secondaryViewController).TopViewController).DetailItem == null)
    {
        // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
        return true;
    }
    return false;
    }
    
  • BruceHamiltonBruceHamilton USMember ✭✭

    Forgot to add the the code that checks for iOS 8 and greater so that primary and secondary get displayed on the iPhone. Here's the code to replace my first code block above:

    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
    {
        SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.Automatic;
        if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
        {
            SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible;
        }
    }
    else if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible;
    }
    
  • BruceHamiltonBruceHamilton USMember ✭✭

    One must need the iPhone model name to determine this, not the iOS version. There are no direct methods to determine that. Disregard the previous post.

Sign In or Register to comment.