Open UISplitViewController to Master View rather than Detail
Swift
UISplitViewController display master view above detail in portrait orientation is not about showing the Master view, it is about presenting the Detail view in full width, underneath the Master view.
UISplitViewController in portrait on iPhone shows detail VC instead of master is about the principle of the collapse mechanism.
This present answer addresses:
- Master → Detail (Compact width)
- iPhone 4s, 5, 5s, SE, 6, 6s, 7 (any orientation)
- iPod Touch
- any iPhone Plus (portrait)
- side-by-side (all other sizes)
- iPad
- any iPhone Plus (landscape)
You must set preferredDisplayMode
. You would want is .primaryVisible
if it existed! Using .allVisible
, iOS picks Detail
if only 1 view fits (Compact width); in that size, the code below will pick Master
.
The trick is to change both the preferredDisplayMode
to .allVisible
and to return true
in collapseSecondary:onto
.
class PrimarySplitViewController: UISplitViewController,
UISplitViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.preferredDisplayMode = .allVisible
}
func splitViewController(
_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController) -> Bool {
// Return true to prevent UIKit from applying its default behavior
return true
}
}
iOS 14
I wasn't getting a callback for splitViewController(_:collapseSecondary:onto:)
and instead used the following new method.
func splitViewController(_ svc: UISplitViewController, topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column) -> UISplitViewController.Column {
return .primary
}