What is Container View in iOS 5 SDK?

前端 未结 2 1338
别那么骄傲
别那么骄傲 2020-12-08 04:38

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

相关标签:
2条回答
  • 2020-12-08 05:08

    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".

    0 讨论(0)
  • 2020-12-08 05:12

    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.

    0 讨论(0)
提交回复
热议问题