URL:
The way that Apple handles this is:
- (BOOL)splitViewController:(UISplitViewController *)splitViewControllercollapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController { if ([secondaryViewController isKindOfClass:[UINavigationController class]] && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]] && ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) { // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded. return YES; } else { return NO; }}
This implementation basically does the following:
If
secondaryViewController
is what we're expecting (aUINavigationController
), and it's showing what we're expecting (aDetailViewController
-- your view controller), but has no model (detailItem
), then "Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
"Otherwise, return "
NO
to let the split view controller try and incorporate the secondary view controller’s content into the collapsed interface"
The results are the following for the iPhone in portrait (either starting in portrait or rotating to portrait -- or more accurately compact size class):
If your view is correct
and has a model, show the detail view controller
but has no model, show the master view controller
show the master view controller
Here is the accepted answer in Swift. Just create this subclass and assign it to your splitViewController in your storyboard.
//GlobalSplitViewController.swiftimport UIKitclass GlobalSplitViewController: UISplitViewController, UISplitViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() self.delegate = self } func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController!, ontoPrimaryViewController primaryViewController: UIViewController!) -> Bool{ return true }}