Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Last active February 27, 2018 21:23
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 StanislavK/ad650a0864f13adec6019d8a3b286ec5 to your computer and use it in GitHub Desktop.
Save StanislavK/ad650a0864f13adec6019d8a3b286ec5 to your computer and use it in GitHub Desktop.
// That’s marked @nonobjc so it won’t conflict with any of Apple’s own code, now or in the future.
@nonobjc extension UIViewController {
func add(_ child: UIViewController, frame: CGRect? = nil) {
addChildViewController(child)
if let frame = frame {
child.view.frame = frame
}
view.addSubview(child.view)
child.didMove(toParentViewController: self)
}
func remove() {
guard parent != nil else {
return
}
willMove(toParentViewController: nil)
view.removeFromSuperview()
removeFromParentViewController()
} }
//usage:
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Second") else { return }
add(vc)
performSlowWork(with: data) {
vc.remove()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment