1
votes

I've seen a bunch of examples on SO that do the same thing in a custom UIStoryBoardSegue's perform method to implement a modal with a transparent background, but when I try doing this, as soon as the animation finishes and presentViewController is called, the modal's black background instantly comes right back. Here's the code:

- (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;

    sourceViewController.modalPresentationStyle = UIModalPresentationCurrentContext;

    [sourceViewController.view addSubview:destinationViewController.view];
    destinationViewController.view.alpha = 0;

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        destinationViewController.view.alpha = 1;
    } completion:^(BOOL finished) {
        [destinationViewController.view removeFromSuperview];

        // boom, the black background returns!
        [sourceViewController presentViewController:destinationViewController animated:NO completion:^{
            // these have no effect
            destinationViewController.view.backgroundColor = [UIColor clearColor];
            destinationViewController.view.superview.backgroundColor = [UIColor clearColor];
        }];
    }];
}

I'm also setting the modal presentationStyle to "Current Context" in the source VC through IB:

enter image description here

I'm not using a UINavigationController.

As you can see I've tried setting the background color of the destination view controller, the superview of the destination view controller, setting .modalPresentationStyle on the sourceViewController and destinationViewController, but nothing seems to work!

If I comment out the part that removes the destination view from its superview, and the presentViewController: call, everything works fine, but then destination VC is dealloc'ed so I'm unable to interact with it and/or unwind it later. Really at a loss here.

Edit: Another issue I'm seeing in the console after going this route is an Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x7f86a042c114>. error.

1

1 Answers

0
votes

Solved:

So it looks like there's no way (as far as I can tell) to perform a custom modal segue with a transparent background completely contained within the perform method.

The solution was to set my custom segue as the transitionDelegate inside perform and then do the animation in animateTransition: instead. Finally, call [transitionContext completeTransition:YES]; after the animation has finished.

After doing this, the "Unbalanced calls..." error also went away.