Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simultaneously present two view controllers?

My goal is to present a view controller without dismissing directly to its presentingViewController.

To make it more clear, consider the following example:

Referring to this storyboard:

enter image description here

Assuming that the first black view controller is the first (initial) one, it should present the third white view controller; The issue is the white view controller should dismisses to the second orange view controller but not the black one, so it should behave like:

  • Black VC Presents White VC.
  • White VC Dismisses to Orange VC.
  • Orange VC Dismisses to Black VC.

How to apply such a behavior?

Remark: There is no navigation controller, it should be present/dismiss, not push/pop.

like image 861
Ahmad F Avatar asked May 02 '17 15:05

Ahmad F


2 Answers

This could be achieved by letting the first black view controller to present the second orange view controller and then the orange view controller should present the third white view controller.

But this arises an issue which is: the end-user will clearly notice that there are two view controllers have been sequentially presented. For solving this, you will need to do a pretty simple trick which is to take a screenshot from the first view controller and passing it to the second view controller to display it while presenting the third view controller.

You can check this repository to see how it is exactly could be done (Swift 3).

The final output would be:

enter image description here

like image 159
Ahmad F Avatar answered Sep 20 '22 04:09

Ahmad F


Here is one method...

  1. Black presents Orange
  2. In viewDidLoad, Orange instantiates White as a childVC and adds White's view as a 'full-screen' subview.
  3. On button tap in White, White's view is animated away and removed from the hierarchy.
  4. On button tap in Orange, Orange is dismissed, returning user to Black.

enter image description here

Here is method two...

  1. Embed Black in a NavigationController without navigation bar
  2. On tap in Black, instantiate Orange and White VCs
  3. Call .setViewControllers:animated: to "stack the views" on the navigation controller, and jump directly to the last view.
  4. After that, navigation backwards uses the standard .popViewController functionality.

enter image description here

You can view a working example of both methods here: https://github.com/DonMag/SkipNavigation

like image 31
DonMag Avatar answered Sep 20 '22 04:09

DonMag