Search code examples
iosobjective-cuisplitviewcontroller

How to close master view controller in splitViewController


I have an app with a split view controller implemented. I also have a setting that overrides the trait collection so all devices are set to regular size classes (instead of compact for iPhones).

I also have a setting in the app that allows users to toggle "split view". This just switches the return value for targetDisplayModeForActionInSplitViewController from UISplitViewControllerDisplayModeAllVisible to UISplitViewControllerDisplayModeOverlay.

Most of my view controllers are loaded through segues in the storyboard, however some are loaded programmatically (ex. [self presentViewController...]).

The problem I am having is that when the display mode is set to Overlay, when any of the views are loaded programmatically, the master view controller does not collapse. I have tried dismissing it with code from similar questions, but to no luck. Does anybody know how to do this?


Solution

  • Figured it out for anybody else with the same problem. Manually setting the preferred display mode to hidden will automatically hide the view controller. In my split view controller class I simply added

    -(void)setDisplayModeHidden {
        self.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
    }
    

    And registered it as an observer in NSNotificationCenter. Whenever I need to close the view controller I just call the function and it will hide it!