Skip to content

Instantly share code, notes, and snippets.

@susieyy
Created January 14, 2017 02:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susieyy/5146868cc71d97ebaa2cb725ee13637e to your computer and use it in GitHub Desktop.
Save susieyy/5146868cc71d97ebaa2cb725ee13637e to your computer and use it in GitHub Desktop.
#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