Created
June 17, 2019 15:17
-
-
Save NeilsUltimateLab/3690c64df13541ac06f51c6a53986d20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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