iOS/Swift: one Container View to hold 3 possible View Controllers

1.7k Views Asked by At

In an area of the app, I would like to show one of 3 possible View Controllers.

I am assuming I can do that by adding a Container View is such area (as shown below)

enter image description here

The questions are:

1) How do I wire the 3 children View Controllers to this Container View in the storyboard? Do I need to connect the Container View to a parent View Controller and then connect this to the 3 children?

2) Which is the Swift code to show one of the 3 View Controllers and hide the other 2?

1

There are 1 best solutions below

13
On BEST ANSWER

I assume that when the user first enters the screen, one of the three view controllers will be there and the user has buttons to switch to a different view controller.

Attach that first VC directly to the container in the storyboard. The other two can be attached to the buttons that cause them to come to the foreground, or attached to the primary VC directly.

You will also need to make a custom segue class (or two?) that knows how to handle the transition from one VC to another.

--- Edit ---

Here is one option as a github repo. Note that if you want to transition between view controllers, you have a bit more work to do, but this should get you started.