Skip to content

Instantly share code, notes, and snippets.

@susieyy susieyy/ChangeRoot.swift Secret
Created Jan 14, 2017

Embed
What would you like to do?
#swtws
guard let window = UIApplication.sharedApplication().appDelegate.window, rootViewController = window.rootViewController else { return }
let newRootViewController = UINavigationController(rootViewController: vc)
// rootViewControllerを差し替える場合は、メモリリーク防止のため事前にdismissを行います
// https://gist.github.com/mono0926/abd7d079361f36efdd9068b27b41ea50
UIView.transitionWithView(window, duration: 0.2, options: .CurveEaseInOut, animations: {
rootViewController.dismissdismissViewControllerAnimated(false, completion: nil)
window.rootViewController = newRootViewController
}) { _ in }
// rootViewControllerを差し替えず、ChildViewControllerの差し替えで行う方法もあります
// refs. http://dealforest.hatenablog.com/entry/2016/12/14/124555
UIView.transitionWithView(window, duration: 0.2, options: .CurveEaseInOut, animations: {
rootViewController.childViewControllers.forEach {
$0.view.removeFromSuperview()
$0.removeFromParentViewController()
}
rootViewController.view.addSubView(newRootViewController.view)
rootViewController.addChildViewController(newRootViewController)
newRootViewController.didMoveToParentViewController(rootViewController)
}) { _ in }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.