Skip to content

Instantly share code, notes, and snippets.

@braking
Last active August 16, 2017 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braking/f48f3c9e722d8b5b7530 to your computer and use it in GitHub Desktop.
Save braking/f48f3c9e722d8b5b7530 to your computer and use it in GitHub Desktop.
Convenience extension on UIViewController for containing child view controllers.
extension UIViewController {
/**
Convenience function to add a child view controller to self.
This will send the proper notifications to the child view controller
and add the child view controller's view to `self.view`.
- parameter child: The view controller that should be contained in `self`.
*/
func addContainedChildViewController(child: UIViewController) {
addContainedChildViewController(child, inView: view)
}
/**
Convenience function to add a child view controller to self.
This will send the proper notifications to the child view controller
and add the child view controller's view to the provided `parentView`.
- parameter child: The view controller that should be contained in `self`.
- parameter parentView: The view in which to add the child's view.
*/
func addContainedChildViewController(child: UIViewController, inView parentView: UIView) {
addChildViewController(child)
parentView.addSubview(child.view)
child.didMoveToParentViewController(self)
}
/**
Convenience function to break containment with a parent view controller.
Calling this on a child/contained view controller will call the
proper methods to have it removed. It will send the proper notifications
to the child view controller.
*/
func breakContainment() {
willMoveToParentViewController(nil)
view.removeFromSuperview()
removeFromParentViewController()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment