I'm confused that why and when we need to use the container view? and how can we instantiate a Container View by code?
Unlike a content view controller that displays your app's data, a container view controller displays other view controllers, arranging them onscreen and handling navigation between them. A container view controller is still a view controller, so you display it in a window or present it like any other view controller.
Container view controllers are a way to combine the content from multiple view controllers into a single user interface. Container view controllers are most often used to facilitate navigation and to create new user interface types based on existing content.
Essentially, container view controllers are subclasses of UIViewController that are dedicated to managing other view controllers, and presenting them in some fashion - just like how UINavigationController presents its children in a navigation stack and manages a navigation bar.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With