0
votes

I’d like to have split views in several places of my iOS app, none of them as the root view controller. I understand that split views are initially designed to sit at the root of the app and provide the root navigation controller, and that Apple’s guidelines initially did not allow any workarounds. Updated guidelines state

You cannot push a split view controller onto a navigation stack. Although it is possible to install a split view controller as a child in some other container view controllers, doing is not recommended in most cases.

Now the split view pattern would really benefit my app, and I don’t want to reinvent the wheel here, so I gave it a try using a container view, mainly using the following steps:

  1. Create a scene with a regular UIViewController.
  2. Add a UIContainerView covering the entire screen.
  3. Add a UISplitViewController to the storyboard, which creates a split view controller, a navigation controller, a table view controller (for the master view), and a regular view controller (for the detail view).
  4. Create an embed segue from the container view to the split view controller.

This has a few quirks, which I hope to iron out eventually (e.g. initially showing detail view, swiping in the table view from the left on an iPad apparently tries to also back nav on the main navigation), but it basically works. So far, so good.

Now, the problem is that I have two navigation controllers: the main navigation controller at the root of my app and the navigation controller in the embedded split view. This gives me two navigation bars with independent navigation, allowing me to:

  • navigate back to the root of the split view using the embedded navigation controller
  • navigate back from the container view in the enclosing navigation controller

Besides two navigation bars not being appealing, I don’t want iPhone users to perform the second directly from the detail view. So my next steps were:

  1. hide the navigation bar in the outer view controller
  2. add a back navigation button to the inner navigation bar to take over the role of the main navigation bar’s back button

Left to do is the implementation for that back button that pops the container's view controller of the main navigation stack. The question is: how can I access the main navigation controller from the embedded view that has its own navigation controller (using Swift)?

2

2 Answers

1
votes

I had to face a similar kind of problem while developing my app. My problem was, I had to display navigation controller and splitviewcontroller on side bar. Again a problem was navigation controller form splitview to navigation controller. Below are the steps which i followed

1) While creating a split view controller, I hided the navigation controller of master and detail and set it to root view, please also keep the reference of your top level navigation controller.

2) I increased the 'y' of splitview.root.window and view to accommodate custom view.

3) I created a custom view with a back button and then handled the transition with animation.

Please let me know if you want code snippets. I would have shared it now. But i have to search for it.

1
votes

Accessing the navigation stack of the parent's (containing view controller's) navigation controller turned out to be straightforward:

@IBAction func backButtonTapped(_ sender: UIBarButtonItem) {
    parent?.navigationController?.popViewController(animated: true)
}