Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a UISplitViewController as a Modal View Controller?

I am trying to display a UISplitViewController presenting it as a Modal View Controller in my iPad app. I manage to have it display, but for some reason there is a gap to the left of the modal view the size of a Status Bar which is also preserved when the orientation is changed.

alt text

Does anybody know why this is happening? Or if this is even possible? Maybe I'm just digging myself a huge hole.

like image 325
A Salcedo Avatar asked Jan 19 '11 19:01

A Salcedo


2 Answers

Like for many of you, I needed a 'modal way' to use the UISplitViewController. This seems to be an old issue, but all I found in StackOverflow was at best an explanation why the problem happens when you attempt to do so (like the accepted answer above), or 'hack-arounds'.

However, sometimes it is also not very convenient to change much of your code-base and make a UISplitViewController the initial object just to get it's functionality up and running.

In turns out, there's a way to make everybody happy (including Apple guidelines). The solution that I found best, was to use the UISplitViewController normally, but when needed to be shown/dismissed, use the following approach:

-(void)presentWithMasterViewController: (UIViewController *) thisMasterViewController
   andDetailViewController: (UIViewController *) thisDetailViewController
                        completion:(void(^)(void))completion
{
    masterViewController = thisMasterViewController;
    detailViewController = thisDetailViewController;

    [self setViewControllers:[NSArray arrayWithObjects:masterViewController, detailViewController, nil]];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

    self.window.rootViewController = self;

    [self.window makeKeyAndVisible];

    if(completion)
        completion();
    }

-(void)dismissViewControllerWithCompletion:(void (^)(void))completion
 {
     self.window = nil;
     masterViewController = nil;
     detailViewController = nil;
     if(completion)
         completion();
 }

Where "window", is a property of your UISplitViewController subclass. And the system will take care of the rest!

For convenience/reference, I uploaded this as a UISplitViewController subclass to gitHub:

ModalSplitViewController

--EXAMPLE ON HOW TO USE --

    mySplitViewController = [[ModalSplitViewController alloc] init];
    mySplitViewController.delegate = self;

    [mySplitViewController presentWithMasterViewController:masterViewController andDetailViewController:detailViewController completion:nil];

    // when done:

    [mySplitViewController dismissViewControllerWithCompletion:nil];
    mySplitViewController = nil;

Side-note: I guess most of the confusion originates from the fact that the UISplitView usage example from Apple documentation uses the window created in the appDelegate, and for the fact that most people are not so familiar with the window concept - because we normally don't need to (they are created once in StoryBoards or boilerplate code).

Additionally, if you are doing state restoration, one should not forget that programmatically-created UIViewControllers won't automatically be restored by the system.

like image 157
Thiago F. Alencar Avatar answered Nov 07 '22 01:11

Thiago F. Alencar


The stock UISplitViewController was designed for use as the root view controller only. Presenting one modally goes against the Apple Human Interface Guidelines and has a high probability of getting rejected by the App Review Team. In addition, you may receive the error:

Application tried to present Split View Controllers modally

like image 6
Evan Mulawski Avatar answered Nov 07 '22 00:11

Evan Mulawski