Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.