3

There are many answers to the complementary question, which is how to prevent a transition to PrimaryOverLay on a from Regular to Compact interface change, eg use

func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool

In my case, I have an iPhone 6+ with the detail view showing in portrait. When I rotate the device to horizontal (Compact to Regular), I want the primary view to stay hidden. I've tried setting the preferredDisplayMode to .PrimaryHidden in many places, but it has no apparent affect. Googling has turned up nothing.

David H
  • 40,852
  • 12
  • 92
  • 138

2 Answers2

3

Well, after I wrote the question, but before posting it, I tripped on a possible solution, which is to override the trait collection that the split view controller references.

I took that idea and decided to subclass UISplitViewController, and override the traitCollection property. That did the trick:

final class MySplitViewController: UISplitViewController {
  var didOnce = false
  override var traitCollection: UITraitCollection {
    let old = super.traitCollection
    let change = UITraitCollection(horizontalSizeClass: .Compact)
    let new = UITraitCollection(traitsFromCollections: [old, change])
    return new
  }

Obviously this is hardcoded for one device - later I'll go and add some functions that I can use to control what is in fact returned.

David H
  • 40,852
  • 12
  • 92
  • 138
1

Don't override traitCollection, instead use the method setOverrideTraitCollection:forChildViewController: in a parent view controller of your split controller, like in Apple's example AAPLTraitOverrideViewController.m

If your split controller doesn't have a parent, making a parent is really easy in the Storyboard. Add a new view controller, make it the entry point, add a container view, delete the default embedded view and instead add an embed segue to the split controller and set the override on self.childViewControllers.firstObject in viewDidLoad.

malhal
  • 26,330
  • 7
  • 115
  • 133
  • 1
    Don’t believe storyboards existed when I wrote this, but agree, your suggestion better than subclassing. – David H Dec 23 '19 at 16:06