Search code examples
iosswiftipaduisplitviewcontroller

Collapse detail view controller on iPad 12.9'' split screen


I am building a History section for my app which uses SplitViewController to display a calendar as Master and stats for certain day as Detail. Everything works great except for one annoying issue: on the big 12.9'' iPad screen, when in landscape mode and split in half, the width of the frame is of wR size class which causes the SplitViewController to display both Master and Detail, with latter squeezed and pretty much unusable:

landscape half split

On the other hand, when in portrait mode and 2/3 split screen - although according to Xcode it's also a wR class - my calendar Master takes up the whole frame, with Detail collapsed:

enter image description here

I would rather prefer this layout in first case as well, but struggle to find a way of force-collapsing the Detail. I tried setting maximumPrimaryColumnWidth to the needed size of 678 pt and more, but it had no effect.

Can somebody please suggest an elegant and practical solution to this?


Solution

  • Yes this is possible, make a parent view controller of the split view controller and use setOverrideTraitCollection(_:forChild:) with compact to make the split collapse when the screen width is a certain size.

    Here is the relevant example from Apple's Adaptive Photos sample:

    https://github.com/ios8/AdaptivePhotosAnAdaptiveApplication/blob/master/AdaptivePhotos/AAPLTraitOverrideViewController.m

    And here is the video of the WWDC 2014 session explaining it:

    https://www.youtube.com/watch?v=lMGnZ5iTmuI