Skip to content

Commit

Permalink
(HeroTransitions#717) Fix issue with deallocating view controller whe…
Browse files Browse the repository at this point in the history
…n replacing root view controller of UIWindow
  • Loading branch information
rafalwojcik committed Apr 11, 2021
1 parent 15e78a2 commit 42be25d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions Sources/Extensions/UIViewController+Hero.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
}
Expand Down

0 comments on commit 42be25d

Please sign in to comment.