10

I've set up a UISplitViewController with style .tripleColumn.

let splitViewController = UISplitViewController(style: .tripleColumn)
preferredDisplayMode = .twoBesideSecondary
preferredSplitBehavior = .tile

For the first menu item "My Stories" it needs three columns. However, for all the other menu items it should only be the primary column (sidebar) and the content visible. The .supplementary column should be hidden.

enter image description here

When clicking on "My stats" for example, the following code hides both the .primary (sidebar) and .supplementary column and only shows "My Stats".

splitViewController?.showDetailViewController(stats, sender: nil)
splitViewController?.hide(.supplementary)

How can I switch to a .twoColumn style and only have the .primary and .secondary column visible?

gpichler
  • 2,181
  • 2
  • 27
  • 52
  • May I ask you how you ended up with your implementation ? I have the exact same requirements with one navigation point needs a triple column layout and all of the others only a double column layout. I saw, from the accepted answer, that the use case is not intended by Apple and therefore I am interested in what alternative UIs are possible. – grahan Jan 15 '21 at 21:15
  • I'm using a `.twoColumn` style and have rebuilt a split view controller in SwiftUI for the only screen that was intended to use a `.threeColumn`. – gpichler Jan 18 '21 at 14:20
  • Gotcha. So in an UIKit world the secondary view controller in your "My Stories" tab would be another UISplitViewController ? – grahan Jan 19 '21 at 07:39
  • I've tried it with a UISplitViewController but what I can remember is that this interfered somehow with the parent UISplitViewController. – gpichler Jan 19 '21 at 12:21
  • I see. So custom solution :) I just tried it with a nested UISplitViewController as the `secondary` view on a `doubleColumn` and it has some animation glitches: https://imgur.com/a/b9vdKsf Indeed it seems that this use case is not intended by by Apple. – grahan Jan 19 '21 at 12:29
  • 2
    The Notes app of Apple seems to toggle between `.tripleColumn` and `.doubleColumn`, when you switch from list view to 'gallery' modus. The question is if this is done with private API's or not. – Ely Jun 14 '21 at 18:02

3 Answers3

4

In a .tripleColumn split view controller, by design, it is impossible for the .primary column to appear without also showing the .supplementary column.

And you cannot change one and the same split view controller from being a .tripleColumn to being a .doubleColumn. I suppose you could just rip the entire split view controller right out of the interface and substitute a different one, but is that really what you want to do? I think it would be better to use the split view controller the way it is designed to be used.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That's already very helpful and got to know. I'm thinking whether it would be possible to change the style to `.twoColumn` and use another `UISplitViewController` as detail view for the "My Stories" tab? – gpichler Nov 23 '20 at 07:58
  • 1
    I am struggling with the exact same issue. Our design calls for the supplementary column to appear only for one of the primary column selections. My current implementation is to recreate the split view controller for each primary mode selection change but it's suboptimal because the entire view is redrawn. I had similar thoughts about using two .twoColumn split view controllers, but that isn't going to keep the nice .tripleColumn behaviors. – pennyowe Nov 24 '20 at 17:07
  • @pennyowe Basically you and the OP have a use case that Apple has not allowed for. Time to file enhancement requests! – matt Nov 24 '20 at 18:29
  • Thanks for the reply @matt - that is basically what I had suspected. In the end we decided to change our application design to add a supplementary column for the view that didn't have one. – pennyowe Nov 25 '20 at 19:42
2

I had similar issue, my solution is pretty "hacky" though. I just change preferred supplementary column width to 1 when I want to hide it. Anything less than 1 causes app to freeze.

func toggleSupplementary(_ isOn: Bool) {
    viewController(for: .supplementary)?.view.alpha = isOn ? 1 : 0
    preferredSupplementaryColumnWidth = isOn ? UISplitViewController.automaticDimension : 1
}

You can also try setting supplementary view controller to nil. That way you will achieve what you wanted.

splitViewController.setViewController(nil, for: .supplementary)

However this is causing crash when your split view controller collapses to compact width and then expands again. Solution with changing column width works in that case too.

Gamec
  • 404
  • 3
  • 8
1

The solution is so simple that I hate having lost a day looking for it (and even reconsidering my entire app design).

To hide the supplementary column, set its view to nil. It's that simple.

splitViewController?.setViewController(nil, for: .supplementary)

And when you need it again: set it to a reasonable view!

Note: the show sidebar button is broken when supplementary is nil. I decided to remove the possibility to hide the sidebar all together like in the Music app. To do that, remove "Present Master with Gesture".