Search code examples
iosswiftuinavigationcontrolleruitabbarcontrolleruisplitviewcontroller

Split view controller navigation bar question


Background:

I have a UISplitViewController showing a UITabBarController as its detail view (.secondary). The UITabBarController shows a UIViewController embedded in a UINavigationController.

The plot thickens:

On iPad (not .compact), this works great. The UINavigationBar of the UINavigationController is shown as there is no push segue from the UISplitViewController as the views are .tiled.

However, on iPhone (.compact environment), this presents a problem. When showDetailViewController: is called, the UISplitViewController pushes the UITabBarController, which results in there being a second redundant UINavigationBar with a “Back” UIBarButtonItem for the UISplitViewController's UINavigationBar, and then right under it is the UINavigationBar of the UINavigationController.

Here’s what I’ve tried:

Removing the UINavigationController from the hierarchy fixes the issue on iPhone (or .compact environments), but introduces an issue on iPad (not .compact): no UINavigationBar from the UISplitViewController is shown at all.

Is there a straightforward solution/protocol for this? I feel as if though this is a pretty common scenario and I’m unsure how to move forward.

The problem:

To reiterate, the goal is to have one UINavigationBar showing on both iPhone (.compact) and iPad (not .compact) for the detail (.secondary) view of my UISplitViewController.


Solution

  • For anyone else facing a similar problem (as any similar questions I've found on this site go unanswered), the solution was to instead embed the UITabBarController into a UINavigationController.

    The UISplitViewController picks up the fact that it is pushing a UINavigationController, and merges the UINavigationBars. In my original setup, the UITabBarController acted as a barrier and prevented the UINavigationBars from merging.