UISplitViewController - dismiss / pop Detail View Controller in code in collapsed mode

On devices which don't support the "split" mode, if

  1. You want to present the master view controller instead of the detail when the UISplitViewController first loads, then returning YES in your delegate class (UISplitViewControllerDelegate) splitViewController:collapseSecondaryViewController:ontoPrimaryViewController: method method should do that:

    - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
        return YES;
    }
    
  2. You want to dismiss the detail view controller back to the master, after a specific event (e.g. a touch on a button). In this case you have to pop the detail view controller navigation controller:

    [detailViewController.navigationController.navigationController popToRootViewControllerAnimated:YES]
    

Had a similar issue today trying to pop back from a detail view in a split view controller.

While I'm sure the accepted answer works fine, another approach I found that works as well and may be a bit cleaner is to use an unwind segue.

I setup an unwind segue on the master view I wanted to return to, then created a segue link to the unwind segue from the view I wanted to pop (note: assumes that you are using storyboards).

Make sure to setup the IBAction on the destination view you are popping back to:

-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue { }

Connect the exit to the segue in the storyboard for the unwind segue. Sorry, I'm not providing a lot of detail on how to setup the unwind segue, but there are many tutorials available for that.

Then on your controller you want to dismiss, connect a segue to the unwind segue of the controller you are popping back to. Be sure to name the segue.

Then on the button touch in the view controller you want to dismiss, just call

[self performSegueWithIdentifier:@"unwindSegueName" sender:self];

This worked really well and avoids digging backwards into a navigation hierarchy that may change.

Hope this is useful to someone! Happy Holidays!


Here's what I ended up doing to pop the DetailVC if we are in a collapsed state (iPhone excluding +sizes), and show/hide the MasterVC if we are not in a collapsed state (iPad).

@IBAction func backTouchUp(_ sender: UIButton) {
    if let splitViewController = splitViewController,
        !splitViewController.isCollapsed {
        UIApplication.shared.sendAction(splitViewController.displayModeButtonItem.action!, to: splitViewController.displayModeButtonItem.target, from: nil, for: nil)
    } else {
        navigationController?.popViewController(animated: true)
    }
}