How to set the display of a UISplitViewController Master View on Device Rotation


  • Share on Pinterest

The UISplitViewController is extremely useful on iPad's and supersized iPhones. For the most part it also works nicely, but there will come a time that you will want to tame the view that appears on the left, commonly referred to as the master view. You mostly see it being used for lists, which is handy for my Code Notes for iOS application.

But it comes with a complication, when a device rotates to landscape I want to set the master view to show by default, and when in portrait I want to have the master view appear as an overlay that can be dismissed to allow the detail view to take up the full screen.

This little code snippet below will do just that, I added it to my master view controller.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    if UIDevice.current.orientation.isLandscape {
        splitViewController?.preferredDisplayMode = .allVisible
    } else {
        splitViewController?.preferredDisplayMode = .primaryOverlay
    }
}

Hopefully this helps you out keeping that master list display mode under control.