Last active
February 27, 2018 21:23
-
-
Save StanislavK/ad650a0864f13adec6019d8a3b286ec5 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
// 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