Search code examples
iosswiftuikituisplitviewcontroller

UISplitViewController: Show detail view initially


I have a UISplitViewController. The master view shows an overview of the app for navigation, and the detail view is what the user will interact with 99% of the time. When the app starts in a horizontally compact environment, it shows the master view, but I want it to show the detail view instead. The user can go back to the master if they want, but the app is actually fully functional without.

How can I get it to show the detail view on boot, instead of the master?

This should be reasonably simple, but I've tried a number of approaches and none are particularly satisfying.

Try 1

In my UISplitViewControllerDelegate:

func primaryViewControllerForCollapsingSplitViewController(_ splitViewController: UISplitViewController) -> UIViewController?
{
    return splitViewController.viewControllers.last
}

This does indeed show the detail view on boot, but it's missing the navigation button that would take the user to the master view. Obviously that's because the detail view hasn't been pushed onto the stack; instead, it's replaced the master completely. I could of course fudge it by adding my own 'back' button - but that seems like a hack (right?).

Try 2

As soon as the app loads and the master is shown, immediately push the detail view using

splitViewController!.showDetailViewController(detailViewController, sender: self)

This will probably work, but it also seems a bit hacky. It's essentially getting the OS to do the wrong thing initially then quickly changing it before the user notices!

So, is there a better way?


Solution

  • Add :

         self.performSegueWithIdentifier("showDetail" , sender:self)
    

    to the viewDidLoad method of your MasterViewController.

    "showDetail" is the identifier from the segue from your master to detail VC.

    it will push the DetailViewController on to the NavigationController stack