Present multiple modal view controllers?

In some situations, you could have each modally presented view controller present the next one.

let window = UIApplication.sharedApplication().keyWindow!
if let modalVC = window.rootViewController?.presentedViewController {
    modalVC.presentViewController(vc, animated: true, completion: nil)
} else {
    window.rootViewController!.presentViewController(vc, animated: true, completion: nil)
}

The trivial way to attempt to achieve that is to just create the VCs you want to present modally and present one after the other. Unfortunately, this is not gonna work. What you get is just the first VC presented, all others just go nowhere. UIKit just won’t cooperate with you here.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This answer might seem a little unconventional, but it works. It lets you "open multiple modals", but only show one at a time. The key is to have just one modal, but use a Tab Bar Controller to switch between View Controllers.

multiple modals

  1. Wire your modal segue from the blue (presenting) View Controller to a modal container (which is just a regular View Controller)
  2. The modal container holds just a Container View, whose sole purpose is to embed a Tab Bar Controller.
  3. Embed a Tab Bar Controller inside the Container View
  4. Each tab can be a separate View Controller, representing "multiple modals"
  5. Set the Tab Bar Controller's Tab Bar to hidden
  6. Switch between modals using tabBarController?.selectedIndex = 1
  7. Close the modal normally with dismissViewControllerAnimated(true, completion: nil)

It would look something like this:

enter image description here


Dismiss all view controllers above the presented view controller.

Apple's documentation follows:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

Dismiss two modal view controllers.

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];

https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#jumpTo_51