Morgan Morgan - 6 months ago 84
Swift Question

Call master view controller from within detail view of split view controller

I have a tab bar controller with 3 tabs, each embedded within its own navigation controller. This works great, but now I’m trying to put the entire tab bar controller within a split view controller.

storyboard

The problem is the navigation on the master view for the split view controllers hides the existing navigation for the views inside the tab bar controller. If I hide the master view navigation, I get my tab bar controller navigation back but then I have no way of getting back to the master list. I can add the button back in programmatically but it only works on the iPad.

navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true


I also tried playing around with the various options for
preferredDisplayMode
, but again, this only works on the iPad. It has no effect on the iPhone. I can add in a custom button programmatically to the view controller within the tab view controller and call an action from there, but I don't know what action to call. I tried calling the action on the
displayModeButtonItem:


splitViewController?.displayModeButtonItem().action


That didn't work either. I would like to be able to just programmatically add back in the behavior of the back button that the split view controller adds on the iPhone.

Answer

I ended up scrapping the split view controller and just using anther tab. However I had a similar problem in another app and I figured out a kind of hacky solution. I embedded the tab bar controller itself inside a navigation controller. This causes multiple navigation controllers to appear so I did a check in viewWillAppear to hide whichever navigation controller is causing the issue.

if let hidden = tabBarController?.navigationController?.navigationBarHidden {
    if hidden == true {
        tabBarController?.navigationController?.navigationBarHidden = false
    }
}

This app has a split view controller as the root view controller and then tab bar controllers in both the master and detail views. Every view is embedded in a navigation controller and I just show or hide the navigation bar depending on if I have a duplicate.

Comments