Press "Enter" to skip to content

Container View in iOS

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.
container2
Resulting (in my case):

Screen Shot 2015-08-09 at 2.59.34 am

Repeat the same step for a second View Controller.
Screen Shot 2015-08-09 at 2.59.56 am

Your Storyboard will more or less look like this:
identifiers

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!

Author: kodesnippets

16 Comments

  1. Guy Cohen
    Guy Cohen January 7, 2016

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

    • Aaqib Hussain
      Aaqib Hussain January 7, 2016

      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. πŸ™‚

  2. Anonymous
    Anonymous March 7, 2016

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

  3. Jeppro
    Jeppro November 11, 2017

    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
      Aaqib Hussain December 17, 2017

      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. πŸ™‚

  4. Dave
    Dave November 18, 2017

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

    • Aaqib Hussain
      Aaqib Hussain December 17, 2017

      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. πŸ™‚

  5. Bivin
    Bivin December 19, 2017

    Thank you. It saved my day πŸ™‚

  6. Frank Miller
    Frank Miller December 17, 2018

    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
      Aaqib Hussain January 13, 2019

      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. πŸ™‚

  7. Faran Mushtaq
    Faran Mushtaq February 25, 2019

    Great one my friend..
    Keep it up ! πŸ™‚

Leave a Reply

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