From bec611c1fc5d0bd6dde79d2f0fc6475cc06cc726 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 2 Aug 2026 16:57:28 -0700 Subject: [PATCH] Fix blank UI after dismissing a full-screen image. Fixes #3641. --- iOS/Article/ImageTransition.swift | 44 ++++++++++++++++++++++----- iOS/Article/ImageViewController.swift | 4 +++ iOS/Article/WebViewController.swift | 17 +---------- iOS/SceneCoordinator.swift | 5 +-- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/iOS/Article/ImageTransition.swift b/iOS/Article/ImageTransition.swift index 3ea1cea1d..2f80054fc 100644 --- a/iOS/Article/ImageTransition.swift +++ b/iOS/Article/ImageTransition.swift @@ -38,8 +38,7 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning { let imageView = UIImageView(image: originImage) imageView.frame = originFrame - let fromView = transitionContext.view(forKey: .from)! - fromView.removeFromSuperview() + transitionContext.view(forKey: .from)?.removeFromSuperview() transitionContext.containerView.backgroundColor = Assets.Colors.fullScreenBackground transitionContext.containerView.addSubview(imageView) @@ -57,25 +56,36 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning { } }, completion: { _ in imageView.removeFromSuperview() - let toView = transitionContext.view(forKey: .to)! - transitionContext.containerView.addSubview(toView) + if let toView = transitionContext.view(forKey: .to) { + transitionContext.containerView.addSubview(toView) + } transitionContext.completeTransition(true) }) } private func animateTransitionReturning(using transitionContext: UIViewControllerContextTransitioning) { - guard let imageController = imageViewController(forKey: .from, in: transitionContext) else { + + // The presenting animation removed the destination view from the window, + // so it must be restored on every path out of here. + guard let toView = transitionContext.view(forKey: .to) else { transitionContext.completeTransition(false) return } + + guard let imageController = imageViewController(forKey: .from, in: transitionContext), + let fromView = transitionContext.view(forKey: .from), + let window = fromView.window else { + transitionContext.containerView.addSubview(toView) + transitionContext.completeTransition(true) + return + } + let imageView = UIImageView(image: originImage) imageView.frame = imageController.zoomedFrame - let fromView = transitionContext.view(forKey: .from)! - let windowFrame = fromView.window!.frame + let windowFrame = window.frame fromView.removeFromSuperview() - let toView = transitionContext.view(forKey: .to)! transitionContext.containerView.addSubview(toView) let maskingView = UIView() @@ -117,3 +127,21 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning { } } + +// The transition is its own transitioning delegate, strongly owned by the presented +// ImageViewController — the WebViewController that configured it can be deallocated +// while the viewer is up, and the dismissal animation must still run to restore the +// view the presenting animation removed. +// +extension ImageTransition: UIViewControllerTransitioningDelegate { + + func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { + self.presenting = true + return self + } + + func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { + self.presenting = false + return self + } +} diff --git a/iOS/Article/ImageViewController.swift b/iOS/Article/ImageViewController.swift index 3d84c6ece..ec0ba7800 100644 --- a/iOS/Article/ImageViewController.swift +++ b/iOS/Article/ImageViewController.swift @@ -19,6 +19,10 @@ final class ImageViewController: UIViewController { var image: UIImage! var imageTitle: String? + + // Strong reference — the navigation controller’s transitioningDelegate is weak, + // and the transition must outlive the WebViewController that configured it. + var transition: ImageTransition? var zoomedFrame: CGRect { return imageScrollView.zoomedFrame } diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index 04b077a5f..c89408536 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -505,21 +505,6 @@ extension WebViewController: WKScriptMessageHandler { } -// MARK: UIViewControllerTransitioningDelegate - -extension WebViewController: UIViewControllerTransitioningDelegate { - - func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { - transition.presenting = true - return transition - } - - func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { - transition.presenting = false - return transition - } -} - // MARK: extension WebViewController: UIScrollViewDelegate { @@ -726,7 +711,7 @@ private extension WebViewController { transition.originImage = image - coordinator.showFullScreenImage(image: image, imageTitle: clickMessage.imageTitle, transitioningDelegate: self) + coordinator.showFullScreenImage(image: image, imageTitle: clickMessage.imageTitle, transition: transition) } func stopMediaPlayback(_ webView: WKWebView) { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index cf14c1bb0..373ba0a0b 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -1486,13 +1486,14 @@ struct SidebarItemNode: Hashable, Sendable { mainFeedCollectionViewController.present(addNavViewController, animated: true) } - func showFullScreenImage(image: UIImage, imageTitle: String?, transitioningDelegate: UIViewControllerTransitioningDelegate) { + func showFullScreenImage(image: UIImage, imageTitle: String?, transition: ImageTransition) { let imageVC = UIStoryboard.main.instantiateController(ofType: ImageViewController.self) imageVC.image = image imageVC.imageTitle = imageTitle + imageVC.transition = transition let navController = UINavigationController(rootViewController: imageVC) navController.modalPresentationStyle = .currentContext - navController.transitioningDelegate = transitioningDelegate + navController.transitioningDelegate = transition rootSplitViewController.present(navController, animated: true) }