Skip to content

Instantly share code, notes, and snippets.

@joamafer
Created January 22, 2017 00:55
Show Gist options
  • Save joamafer/56368f55131f2e4170d9ac482b7c0064 to your computer and use it in GitHub Desktop.
Save joamafer/56368f55131f2e4170d9ac482b7c0064 to your computer and use it in GitHub Desktop.
Add child UIViewController to UIView
func add(viewController: UIViewController, toContainerView containerView: UIView) {
viewController.view.translatesAutoresizingMaskIntoConstraints = false
addChildViewController(viewController)
containerView.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
viewController.view.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
viewController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
viewController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
viewController.view.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment