From f76f85e952c1a3a7cf8a82f452f26941ccd0f949 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Fri, 21 Nov 2025 12:24:45 +0800 Subject: [PATCH] Use snapshot placeholder to fix empty screenshot when navigating back --- ios/App/App/AppDelegate.swift | 13 +++++++++++-- ios/App/App/SharedWebViewController.swift | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ios/App/App/AppDelegate.swift b/ios/App/App/AppDelegate.swift index 334f2e39e3..f2635f81d2 100644 --- a/ios/App/App/AppDelegate.swift +++ b/ios/App/App/AppDelegate.swift @@ -199,8 +199,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDel pathStack[pathStack.count - 1] = toVC.targetPath } - // Move shared webview to destination before pop completes - SharedWebViewController.instance.attach(to: toVC) + // Move shared webview to destination before pop completes, but leave a snapshot + // on the previous screen so the transition doesn't show a blank page. + SharedWebViewController.instance.attach( + to: toVC, + leavePlaceholderInPreviousParent: fromVC != nil + ) // Trigger browser back so Reitit updates route-match while attached to destination if let webView = SharedWebViewController.instance.bridgeController.bridge?.webView, @@ -211,11 +215,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UINavigationControllerDel // Fallback: ask JS to render without adding history ignoreRoutePopCount += 1 } + + coordinator.animate(alongsideTransition: nil) { _ in + SharedWebViewController.instance.clearPlaceholder() + } } } func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { guard let current = viewController as? NativePageViewController else { return } + SharedWebViewController.instance.clearPlaceholder() SharedWebViewController.instance.attach(to: current) attachNavigationSwipeGesture() updateSidebarGestureAttachment(for: current) diff --git a/ios/App/App/SharedWebViewController.swift b/ios/App/App/SharedWebViewController.swift index ab69c51fba..f93899fae9 100644 --- a/ios/App/App/SharedWebViewController.swift +++ b/ios/App/App/SharedWebViewController.swift @@ -7,6 +7,7 @@ // import Foundation +import UIKit import Capacitor @objc class SharedWebViewController: NSObject { @@ -19,11 +20,22 @@ import Capacitor }() private weak var currentParent: UIViewController? + private weak var placeholderView: UIView? - func attach(to parent: UIViewController) { + func attach(to parent: UIViewController, leavePlaceholderInPreviousParent: Bool = false) { let vc = bridgeController guard currentParent !== parent else { return } + if leavePlaceholderInPreviousParent, + let previous = currentParent, + placeholderView == nil, + let snapshot = vc.view.snapshotView(afterScreenUpdates: false) { + snapshot.frame = previous.view.bounds + snapshot.autoresizingMask = [.flexibleWidth, .flexibleHeight] + previous.view.addSubview(snapshot) + placeholderView = snapshot + } + // Detach from previous parent if let previous = currentParent { vc.willMove(toParent: nil) @@ -39,4 +51,9 @@ import Capacitor parent.view.addSubview(vc.view) vc.didMove(toParent: parent) } + + func clearPlaceholder() { + placeholderView?.removeFromSuperview() + placeholderView = nil + } }