0

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 Answers2

1

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)
}
0

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.

Nex Mishra
  • 774
  • 7
  • 13
  • Thank you for your answer. I'm not sure if I understand, e.g. I'm having difficulties picturing a split view inside a side bar (menu?). So before you go through the trouble of digging out that code let's make sure it applies. Is your split view's navigation controller embedded in a container view? Are you trying to access the outer navigation controller from within the embedded navigation controller? If yes, it would indeed be nice to see the code. –  Aug 09 '17 at 08:46
  • What i have done is, I am switching my root view controller with transitions. While doing so, I am keeping referance of my outer navigation controller – Nex Mishra Aug 09 '17 at 12:20
  • Yes, I was thinking something along that way. But at least in my case there seems to be a simpler solution. Thanks for your comments (+1)! –  Aug 11 '17 at 05:27
  • Thanks for(+1). My problem was pretty huge. I had to handle a side menu to. So in order to do that, I had to create a side bar menu (Hamburger Menu) from scratch. Any ways thanks – Nex Mishra Aug 11 '17 at 08:09