Search code examples
iosuikitautolayoutuisplitviewcontrollersize-classes

How do I prevent my UISplitViewController from displaying the master VC when rotating to portrait?


I have a UISplitViewController that is used to edit some text.

The flow is basically like this:

  1. The user creates a new note.
  2. The user is transitioned to the Detail View Controller to create or edit said note.
  3. User can edit the text in either portrait or landscape.

enter image description here enter image description here enter image description here

The problem is, when the user is editing the contents in landscape in the detail view controller and he suddenly rotates the device to portrait, the displayed VC will be the master VC instead of the Detail VC (seen in picture 3). I'd like to stay at the detail VC.

I was looking at Apple's Notes.app and when you rotate the device to portrait when editing a note you will stay at the detail VC instead of going back to Master. So this must definitely be possible.

Is there an specific property I need to modify for this? Sadly I was looking at the docs and I couldn't find anything related.


Solution

  • It is typical of me to solve my issues 5 minutes after asking them on SO.

    Turns out the splitViewController:collapseSecondaryViewController:ontoPrimaryViewController UISplitViewController delegate method is in charge of choosing the "collapse" of the views. What originally threw me off is that I didn't consider going back to portrait as something that would be a "collapse".

    So I edited the delegate method with this:

    func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController:UIViewController!, ontoPrimaryViewController primaryViewController:UIViewController!) -> Bool {
        if let secondaryAsNavController = secondaryViewController as? UINavigationController {
            if let topAsDetailController = secondaryAsNavController.topViewController as? TextEditorViewController {
                if topAsDetailController.inEditMode {
                    return false
                }
            }
        }
        return true
    }
    

    The text editor view "won't collapse onto the master view" when rotating to portrait now.