Child View Controller. Why use them ?

Ever wondered why you would go through the hassle of adding a new view controller instead of just using a UIView. This is how you would be adding a child view controller and removing them.

currentController.willMove(toParent: nil)
if childController != nil {
	parentController.view .addSubview(childController!.view)
	childController!.didMove(toParent: parentController)

Why use them ?

  • By adding as a child view controller, you don’t need to set the frame of the view you just loaded. The OS will take care of this. If you had used UIView, the frame size would need to be set manually.
  • When adding as a view controller, you can use it in many different ways. For e.g. you can push it on to a navigation stack, or present it as a modal. You don’t need to worry about any animation.
  • I like this personally, you can get access to view controller methods. e.g. viewWillAppear, viewDidLoad..etc.


Leave a Reply

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