Skip to content

Instantly share code, notes, and snippets.

@jaminguy
Created October 6, 2015 22:04
Show Gist options
  • Save jaminguy/8b76393fed4c66988cc2 to your computer and use it in GitHub Desktop.
Save jaminguy/8b76393fed4c66988cc2 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
protocol ViewControllerContaining {
var containerView: UIView! { get set }
func addViewController(childViewController: UIViewController)
func removeViewController(childViewController: UIViewController)
func addChildViewController(childController: UIViewController)
}
extension ViewControllerContaining {
func addViewController(childViewController: UIViewController) {
self.addChildViewController(childViewController)
childViewController.view.frame = containerView.bounds
containerView.addSubview(childViewController.view)
childViewController.didMoveToParentViewController(self as? UIViewController)
}
func removeViewController(childViewController: UIViewController) {
childViewController.willMoveToParentViewController(nil)
if childViewController.isViewLoaded() {
childViewController.view.removeFromSuperview()
}
childViewController.removeFromParentViewController()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment