Skip to content

Instantly share code, notes, and snippets.

@NeilsUltimateLab
Created June 17, 2019 15:17
Show Gist options
  • Select an option

  • Save NeilsUltimateLab/3690c64df13541ac06f51c6a53986d20 to your computer and use it in GitHub Desktop.

Select an option

Save NeilsUltimateLab/3690c64df13541ac06f51c6a53986d20 to your computer and use it in GitHub Desktop.
import UIKit
protocol ContainmentProvider: class {
func addChild(_ viewController: UIViewController, to containerView: UIView)
func removeChild(_ viewController: UIViewController?)
}
extension ContainmentProvider where Self: UIViewController {
func addChild(_ viewController: UIViewController, to containerView: UIView) {
self.addChild(viewController)
viewController.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(viewController.view)
viewController.view.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
viewController.view.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
viewController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
viewController.view.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
viewController.didMove(toParent: self)
}
func removeChild(_ viewController: UIViewController?) {
viewController?.willMove(toParent: nil)
viewController?.view.removeFromSuperview()
viewController?.removeFromParent()
viewController?.didMove(toParent: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment