Last active
August 16, 2017 02:39
-
-
Save braking/f48f3c9e722d8b5b7530 to your computer and use it in GitHub Desktop.
Convenience extension on UIViewController for containing child view controllers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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