Last active
October 24, 2018 12:30
-
-
Save chriswill0w/c297e04e8674ae70a1bc95fc65962c6b to your computer and use it in GitHub Desktop.
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
import UIKit | |
import PureLayout | |
extension UIViewController { | |
func addChildController(_ controller: UIViewController, containerView: UIView) { | |
addChild(controller) | |
// When your custom container calls the addChild(_:) method, it automatically calls the willMove(toParent:) method of the view controller to be added as a child before adding it. | |
containerView.addSubview(controller.view) | |
controller.view.translatesAutoresizingMaskIntoConstraints = false | |
controller.view.autoPinToSuperviewEdges() | |
controller.didMove(toParent: self) | |
} | |
func removeAsChild() { | |
willMove(toParent: nil) | |
view.removeFromSuperview() | |
removeFromParent() | |
// The removeFromParent() method automatically calls the didMove(toParent:) method of the child view controller after it removes the child. | |
} | |
func tryRemovingFirstChild() { | |
guard let child = children.first else { return } | |
child.removeAsChild() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment