Hello everyone!
I hope everyone would be doing great.

I googled for a long time but I couldn’t find any good tutorial for Container View, I read a tutorial where two Container Views were stacked over one another to achieve the required task, which is certainly not the proper way to do it.

Anyway, I am going to do a tutorial on Container View. So, Lets get started without wasting any further time.

Create a new project and drag a Container View along with two Buttons over a View Controller.
container

Drag a View Controller and connect Container View Controller with an Empty Segue to it. After that select the newly added View Controller open the Size Inspector and set the Simulated Size to Freeform and adjust the width and height of the controller according to the Container View Controller.

Resulting (in my case):

Repeat the same step for a second View Controller.

Your Storyboard will more or less look like this:

After that, add three classes of type UIViewController, i.e two for child view controllers(Let’s say FirstViewController.swift and SecondViewController.swift) and one for container view controller(ContainerViewController.swift), also add a class of type UIStoryboardSegue. Add all these classes to View Controllers and Empty Segues on storyboard, respectively.
When done adding classes, give identifiers to Embedded and Empty segues.

Open up your ViewController.swift class, create two action functions of the buttons that you added in the start, and paste the following code inside:

In your ContainerViewController.swift, paste the following code:

And in your Empty Segue class:

After that you’re good to go.
Here is a video tutorial for this.

I have uploaded this project on Github for reference.
Cheers!

Share:

administrator

16 Comments

  • dineshramitc, August 13, 2015 @ 8:07 am Reply

    Reblogged this on Dinesh Ram Kali..

  • Guy Cohen, January 7, 2016 @ 4:36 am Reply

    Just wondering, is this is the right way to do it ?

    • Aaqib Hussain, January 7, 2016 @ 3:21 pm Reply

      I don’t know if its the right way because only an official Apple developer can tell that. but what I can tell you is that its the only proper way you would find on internet. 🙂

  • Anonymous, March 7, 2016 @ 12:01 pm Reply

    Thank you for this great job ! : )
    I’m new to Apple-develop and do you have a objective-c sample there?

  • Jeppro, November 11, 2017 @ 3:30 pm Reply

    Hi! Thank you for this awesome tutorial. I am new to coding and this has been a great help. I am now trying to figure out how to put some animations between the new and old viewcontrollers, but I can’t figure out where to put the code exactly. I’ve tried in the perform() inside the custom segue based on other sources, but that doesn’t seem to work so far and I get several errors like: “Application tried to present modally an active controller”. Can you help me with that?

    • Aaqib Hussain, December 17, 2017 @ 7:15 pm Reply

      Hi Jeppro, I am sorry for the late reply but I have updated the tutorial and my Github repo with the animation code, kindly check. Thanks. 🙂

  • Dave, November 18, 2017 @ 11:06 pm Reply

    How would you go about this is you wanted to animate the transition in views?

    • Aaqib Hussain, December 17, 2017 @ 7:14 pm Reply

      Hi Dave, I am sorry for the late reply but I have updated the tutorial and my github with the animation code kindly check. Thanks. 🙂

  • Bivin, December 19, 2017 @ 6:26 pm Reply

    Thank you. It saved my day 🙂

  • Frank Miller, December 17, 2018 @ 2:33 am Reply

    How can you access elements on the containerViews from the parent?

    Like if each container has an array of values I want to obtain from each child containerView?

    • Aaqib Hussain, January 13, 2019 @ 9:11 pm Reply

      If you got to the github link. I have added an example to access things. you can look at the sample project. Hope this helps. 🙂

  • Faran Mushtaq, February 25, 2019 @ 12:41 pm Reply

    Great one my friend..
    Keep it up ! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *