Use snapshot placeholder to fix empty screenshot when navigating back

This commit is contained in:
Tienson Qin
2025-11-21 12:24:45 +08:00
parent 0a5ebf97d1
commit f76f85e952
2 changed files with 29 additions and 3 deletions

View File

@@ -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)

View File

@@ -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
}
}