From 42be25d72217e3b74f1eabee7ddbf6510368f5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Wo=CC=81jcik?= Date: Sun, 11 Apr 2021 20:33:05 +0200 Subject: [PATCH] (#717) Fix issue with deallocating view controller when replacing root view controller of UIWindow --- .../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 + } } } }