Container View Controller Examples
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A container view controller is a parent controller whose main job is to host and coordinate child controllers. UIKit already uses this pattern in UINavigationController and UITabBarController, but you can build a custom container when your app needs a layout or interaction model that the built-in containers do not provide.
When to Use a Custom Container
Custom containers make sense when one screen is really a composition of multiple independently managed pieces.
Common examples include:
- a split layout with a menu and detail area
- a dashboard made of reusable child sections
- a host controller that swaps different content controllers into one region
If simple push navigation or tabs already solve the problem, stick with the built-in container types. A custom container is worthwhile only when the behavior is genuinely custom.
The Correct Embed Sequence
The most important rule is that you must create a real parent-child relationship, not just add one controller's view inside another.
The correct sequence is:
- call
addChild - add the child view to the hierarchy
- size or constrain the child view
- call
didMove(toParent:)
Here is a minimal example:
This is the smallest correct version of custom containment in UIKit.
Swapping One Child for Another
Many containers show one child at a time and replace it when state changes. In that case, remove the old child cleanly and finish the new one properly.
Using transition(from:to:duration:options:animations:completion:) keeps the lifecycle flow aligned with the view animation.
Example Designs
A few common custom-container patterns are:
- side menu host
- paged content host
- dashboard shell with permanent child regions
In each case, the parent controller owns layout and routing while each child owns its own screen logic. That separation is the main architectural benefit of containment.
Built-In Containers Still Matter
Do not build a custom container just because you can. UIKit already gives you:
- '
UINavigationControllerfor drill-down flows' - '
UITabBarControllerfor top-level sections' - '
UISplitViewControllerfor multi-column layouts'
Use a custom container only when those controllers do not express the interaction cleanly.
Common Pitfalls
- Adding a child controller's view without calling
addChildanddidMove(toParent:). - Forgetting
willMove(toParent: nil)andremoveFromParent()when removing a child. - Managing containment correctly but forgetting to constrain the child view, which creates layout bugs.
- Building a custom container when a standard UIKit container already matches the design.
- Letting the parent controller absorb all child logic instead of keeping responsibilities separated.
Summary
- A container view controller hosts child controllers, not just child views.
- Use the full containment API:
addChild, add the view, constrain it, then calldidMove(toParent:). - Remove children cleanly when swapping content.
- Custom containers are useful for app-specific composition patterns such as side menus or dashboards.
- Prefer built-in UIKit containers unless your layout or interaction model truly requires a custom parent.

