Skip to content

Instantly share code, notes, and snippets.

@chriswill0w
Last active October 24, 2018 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswill0w/c297e04e8674ae70a1bc95fc65962c6b to your computer and use it in GitHub Desktop.
Save chriswill0w/c297e04e8674ae70a1bc95fc65962c6b to your computer and use it in GitHub Desktop.
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