BBruce
BBruce

Reputation: 265

iOS Presenting a view over a UISplitView Possible?

I have been struggling to achieve a specific view presentation. In my app, I am using a UISplitViewController to present a menu and it's contents. I would like to have a custom presentation of a view over the entire view as shown.

enter image description here

The message view and dimming view are for informational content and will automatically dismiss after a set time period. Can this be done from my UISPlitViewController.m?

I have tried a couple approaches already with out the desired result.

  1. Create a segue from the Splitview to the message view and invoking the segue from viewDidLoad / viewDidAppear:

    [self performSegueWithIdentifier:@"showMessageView" sender:self];
    

This loads the view in the detail view.

  1. Try to instantiate from code in viewDidAppear::

    MessageViewController *messageVC = [self.storyboard instantiateViewControllerWithIdentifier:@"messageView"];
    [self addChildViewController: messageVC];
    [self.view addSubview: messageVC.view];
    [self presentViewController: messageVC animated:YES completion:nil];
    

This results in a crash:

Application tried to present modally an active controller

Upvotes: 0

Views: 164

Answers (2)

ar34z
ar34z

Reputation: 2669

I was facing a similar issue: once the dimmed overlay is fully visible (show animation has finished), the UISplitViewController behind it would disappear.

My trick is to set the modalPresentationStyle to UIModalPresentationOverFullScreen of the VC that's being presented directly after creating it.

Following is my code:

UIViewController* customShapeController = [self.storyboard instantiateViewControllerWithIdentifier:@"CustomShapeController"];
customShapeController.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:customShapeController animated:YES completion:nil];

This works with segues too. Make sure the segue is set to "Present Modally" in your Storyboard. Then in your detail VC call the segue:

[self performSegueWithIdentifier:@"CustomDrawingSegue" sender:sender];

Then prepareForSegue: is called where you can set the modalPresentationStyle property on the destination VC:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *dest = (UIViewController *)segue.destinationViewController;
    dest.modalPresentationStyle = UIModalPresentationOverFullScreen;
}

Upvotes: 1

matt
matt

Reputation: 535232

[self addChildViewController: messageVC];
[self.view addSubview: messageVC.view];
[self presentViewController: messageVC animated:YES completion:nil];

Whoa whoa whoa. You're just confused here. You don't add a child view controller and present it. You do one or the other. (That's exactly what the runtime is slapping you down for.)

My suggestion would be just to present it. Works fine. Remember to present from the root view controller itself, so that the resulting presented view takes over the whole screen.

Upvotes: 0

Related Questions