30

I'm confused that why and when we need to use the container view? and how can we instantiate a Container View by code?

William X
  • 6,751
  • 6
  • 31
  • 50

2 Answers2

38

The container view is a view that you can drag into one of your view controllers that you already have in your storyboard (we'll call this viewControllerA). You automatically get a view controller connected to this view via an embed segue. This new view controller has its frame set, so that it's the same size as the container view -- if you resize the container view, the controller will automatically resize as well. So, if you want, you can drag in multiple container views into viewControllerA, and each will have its own view controller. In code, if you need to access these embedded view controllers, they can be accessed from viewControllerA.childViewControllers -- that will give you an array of any embedded view controllers that you have.

There is a discussion of these container views in the WWDC 2012 Session Videos video called "Adopting Storyboards in Your App".

Undo
  • 25,519
  • 37
  • 106
  • 129
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Is this the same thing as "containing controller" in writing programatically? – huggie Sep 21 '13 at 02:40
  • 2
    @huggie, Using a container view in IB gives you the same thing that you get by using the custom container controller api in code. – rdelmar Sep 21 '13 at 04:14
31

I'm confused that why and when we need to use the container view?

When people talk about container views, they usually mean just a plain old UIView that contains other views. Using a view in that way lets you move all the views it contains as a group, so that their positions relative to each other are maintained. It also makes it easy to hide all the contained views as a group.

and how can we instantiate a Container View by code?

Same way you'd instantiate a UIView from code normally:

CGRect frame = CGRectMake(someX, someY, someWidth, someHeight);
UIView *container = [[UIView alloc] initWithFrame:frame];

After that, you'll probably want to add some subviews to the container, and eventually add the container as a subview of your view controller's view.

Also, note that we're talking about views and not view controllers here. People also talk about container view controllers, by which they mean view controllers that can manage other view controllers. UITabBarController, UINavigationController, and UISplitViewController are examples of the container view controllers that are provided by iOS. You can create your own if you want, but that's a topic for another question.

Update: From your comment, you're apparently wondering about the "Container View" item in the storyboard editor. If you drag one into a view, you'll see that:

  1. The view itself is a UIView used as a placeholder.

  2. Along with the view, the editor creates an area where you can edit the content to be managed by a child view controller. See the picture below.

container view in IB

This isn't just one object -- it's several. You get a view, a child view controller, and an 'embed' segue. You can certainly create those in code yourself and connect them appropriately if you want to.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I don't think this is what the op is asking about. There is a new type of view available in IB (in a storyboard only I think) that can contain a view controller. That view controller is hooked up via an embed segue. This is different from a normal view and from a container view controller. What exactly this thing is behind the scenes, I don't know -- I can't find any documentation on it. I would assume that it can be created in code, but I have no idea how. – rdelmar Nov 13 '12 at 03:38
  • @rdelmar, yes, I'm asking about the "container view" available in IB. I'm confused between the "container view" and a common "view". As per my understanding, view can also contains sub-views and those sub-views can be managed by their own controllers too. May be my understanding is wrong, please kindly help to clarify. – William X Nov 13 '12 at 03:40
  • 1
    Both are the great answers for me. Since @rdelmar responded with the more accurate answer earlier, I'll mark his/her response as the answer. Thanks so much, gurus! – William X Nov 13 '12 at 07:01