From 9f8fe2bb645439a30efa34bdada664ccdf567f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20W=C3=B3jcik?= Date: Fri, 6 May 2022 07:36:20 +0300 Subject: [PATCH] (#717) Fix issue with deallocating view controller when replacing root view controller of UIWindow (#718) --- .../Extensions/UIViewController+Hero.swift | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Sources/Extensions/UIViewController+Hero.swift b/Sources/Extensions/UIViewController+Hero.swift index 8c0a270c..16af8ae4 100644 --- a/Sources/Extensions/UIViewController+Hero.swift +++ b/Sources/Extensions/UIViewController+Hero.swift @@ -305,7 +305,7 @@ public extension HeroExtension where Base: UIViewController { } /** - Replace the current view controller with another VC on the navigation/modal stack. + Replace the current view controller with another VC on the navigation/modal/root view of UIWindow stack. */ func replaceViewController(with next: UIViewController, completion: (() -> Void)? = nil) { let hero = next.transitioningDelegate as? HeroTransition ?? Hero.shared @@ -324,21 +324,19 @@ public extension HeroExtension where Base: UIViewController { hero.forceNotInteractive = true } navigationController.setViewControllers(viewControllers: vcs, animated: true, completion: completion) - } else if let container = base.view.superview { - let parentVC = base.presentingViewController + } else if let container = base.view.superview, let parentVC = base.presentingViewController { hero.transition(from: base, to: next, in: container) { [weak base] finished in - guard let base = base else { return } - guard finished else { return } - + guard let base = base, finished else { return } next.view.window?.addSubview(next.view) - if let parentVC = parentVC { - base.dismiss(animated: false) { - parentVC.present(next, animated: false, completion: completion) - } - } else { - parentVC?.view.window?.rootViewController = next + base.dismiss(animated: false) { + parentVC.present(next, animated: false, completion: completion) } } + } else if let baseWindow = base.view.window, baseWindow.rootViewController == base { + hero.transition(from: base, to: next, in: baseWindow) { [weak base] finished in + guard let base = base, finished else { return } + baseWindow.rootViewController = next + } } } }