Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Since iOS8 we're allowed to use UISplitViewController on both compact and regular devices. This is great because I don't have to create two different storyboard for iPhone and iPad, but there's one problem that I'm stuck with.

If the split view controller is on iPad(if the collapsed property is NO), I can simply call this to show MasterVC on the left side.

self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
[self.splitViewController.displayModeButtonItem action];

But if it's on iPhone(if the collapsed property is YES), the displayMode is ignored, and doesn't do anything.

I cannot pop DetailVC with popToRootViewControllerAnimated because DetailVC has it's own navigation controller.

How does Apple expect us to show MasterVC(dismiss DetailVC) in code in collapsed mode if there isn't any method like dismissViewControllerAnimated:completion: for view controller that was presented with showDetail? Your help will be appreciated. Thanks

like image 483
SFF Avatar asked May 08 '15 08:05

SFF


2 Answers

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]
    
like image 54
pNre Avatar answered Oct 15 '22 08:10

pNre


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)
    }
}
like image 40
Cody Avatar answered Oct 15 '22 10:10

Cody