Present multiple modal view controllers?

和自甴很熟 提交于 2019-11-30 04:57:32
Gtx

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();

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)
}
Rei Kubonaga

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

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.

  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:

Set the correct modalPresentationStyle on both the navigationController AND the viewController itself.

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
vcToPresent.modalPresentationStyle = UIModalPresentationFormSheet;
vcToPresent.preferredContentSize = CGSizeMake(650, 450);

[self presentViewController:vcToPresent animated:YES completion:^{
    self.navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
}];`

I found a solution: provide your own custom animation:

    let transition: CATransition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionReveal
    transition.subtype = kCATransitionFromBottom
    self.view.window!.layer.add(transition, forKey: nil)
    self.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!