Skip to content

Instantly share code, notes, and snippets.

View henning-jh's full-sized avatar

Henning Hoffmann henning-jh

View GitHub Profile
@henning-jh
henning-jh / embedding_view_controllers.md
Last active March 19, 2018 13:37
Embedding View Controllers

In John Sundell’s excellent article Using child view controllers as plugins in Swift, Mr. Sundell reminds us that Apple has given us the ability to compose UIViewController objects together, allowing multiple controllers to display in one view. He also gave us two helper functions to use, one to add a child view controller and the other to remove it. Here is the the function to add a child controller to another controller:

func add(_ child: UIViewController) {
    addChildViewController(child)
    view.addSubview(child.view)
    child.didMove(toParentViewController: self)
}