Search code examples
iosuisplitviewcontroller

UISplitViewController how to show the master view?


I need a ridiculously simple thing - in one of the detail views of my UISplitViewController I have a button. Clicking it should show/open master view. That's it. Is it even possible?

P.S. it should work for all the layouts (iphone & ipad) and orientations. Even if the detail view part is a navigation and I am deep inside several pages, just want to open master view. You can assume iOS8+.


EDIT: Just to clarify what I meant by "deep inside several pages". Here is my storyboard screenshot: enter image description here

Suppose I have a button in Detail Page 2 which should show the master. Setting the preferredDisplayMode works only for non-compact sizes, like iPad. On iPhone 6, for example, nothing changes after setting it. The back button points on Detail Page 1 so even swiping doesn't open master, it goes to previous page in detail navigation. I noticed that in this mode there is no split view at all, it is simulated by a navigation controller. So the questions is: is what I need possible at all or am I wrong trying to conceptually treat it as a "left drawer" which can be opened in any case and device?


Solution

  • At iOS8+ you can change visibility of the master view using an animatable property preferredDisplayMode

    @property (nonatomic) UISplitViewControllerDisplayMode preferredDisplayMode
    

    Universal way to change visibility for all iOS versions is overriding delegate method

    - (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
    {
        return _needsHideMasterView;
    }
    

    Here _needsHideMasterView is BOOL ivar which can be changed in your code to hide master view. For example,

    - (void)hideMasterView:(BOOL)needsHide
    {
        _needsHideMasterView = needsHide;
    
        [splitViewController.view setNeedsLayout];
        [splitViewController.view layoutIfNeeded];
    }