gultekingokhan
2/22/2019 - 12:43 PM

This code snipped makes adding or removing a child view controller easier.

This code snipped makes adding or removing a child view controller easier.

extension UIViewController {
    func add(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        removeFromParent()
        view.removeFromSuperview()
    }
}