iosuiviewcontrollertouchchildviewcontrollerparentviewcontroller

How to create transparent, modal UIViewController on iPhones iOS7+?


I have a relatively simple question that I can't seem to figure out.

I'm inside of a UIViewController (myViewController) that's embedded somewhere inside a UIViewController hierarchy (somewhere underneath a parent UITabBarController).

myViewController has a button. When that button is pressed, I want to overlay the entire screen (including all the goo from the parent UITabBarController) with a black, translucent, modal UIViewController (myModalViewController) with other goo on it.

If I just call

[myViewController presentViewController:myModalViewController..]

the visuals of myViewController disappears as soon as the transition is over, thereby defeating the point of a translucent overlay.

If I set myModalViewController as the child view controller of myViewController and add myModalViewController.view as the subview of myViewController.view, the modal view controller with the translucent black background appears UNDERNEATH all the buttons of the UITabBarController. That's bad, because I want the entire screen to be overlaid.

So, instead, I look for the top-most view controller in the hierarchy and add myModalViewController as the child of it thusly:

UIViewController* root = myViewController;
while (root.parentViewController) {
    root = root.parentViewController;
}

[root addChildViewController:myModalViewController];
[root.view addSubview:myModalViewController.view];

If this the right way to handle this scenario? It seems kludgy.

Also, how do I make myModalViewController behave modally and swallow all touch gestures so that all the UI underneath it doesn't respond to touches? Right now it does and all of my attempts to fix it have failed. Also myModalViewController and all of the UIViews in it don't appear to be receiving any touch notifications what's-so-ever.


Solution

  • If you want transparent ViewController you must use UIModalPresentationCustom Something like this...

    ...
    [yourModalCtrl setModalPresentationStyle:UIModalPresentationCustom];
    [yourModalCtrl setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self setModalPresentationStyle:UIModalPresentationCurrentContext]; //new
    //yourModalCtrl.modalPresentationCapturesStatusBarAppearance = YES;
    [self presentViewController: yourModalCtrl animated:Yes completion:completion];
    

    [self presentViewController:....] or what is your "Presenting controller"...

    http://b2cloud.com.au/how-to-guides/invisible-background-modal-view/

    update

    for compatibility with iOS 7 you need to add new [self setModalPresentationStyle:UIModalPresentationCurrentContext]; - check the all code. Also, you have to set animate : No (it's working with Yes but You will have a message in console :)