From 142fbab693884993755f02fda831844faab6a3f8 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 10 Nov 2026 15:03:40 -0800 Subject: [PATCH] Keep the reader's place in an article when the split view changes display mode. Fixes #5542. --- iOS/Article/WebViewController.swift | 48 +++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index 48ca738ee..26ced38fe 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -85,6 +85,7 @@ final class WebViewController: UIViewController { } private var restoreWindowScrollY: Int? private var isChangingSize = false + private var lastLayoutWidth: CGFloat = 0 private var isArticleContentJavascriptEnabled = AppDefaults.isArticleContentJavascriptEnabled override func viewDidLoad() { @@ -113,17 +114,20 @@ final class WebViewController: UIViewController { override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) + beginSizeChange(coordinator: coordinator) + } - // Ignore mid-transition offsets, then scroll back to the reader’s place. - isChangingSize = true - coordinator.animate(alongsideTransition: nil) { [weak self] _ in - guard let self else { - return - } - restoreScrollAnchor() - scrollPositionDidChange() - isChangingSize = false + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + + // Split view display mode changes and full-screen modal dismissals change the + // article’s width without calling viewWillTransition. + // + let width = view.safeAreaLayoutGuide.layoutFrame.width + if lastLayoutWidth > 0 && width != lastLayoutWidth { + beginSizeChange(coordinator: transitionCoordinator) } + lastLayoutWidth = width } override func viewWillDisappear(_ animated: Bool) { @@ -588,6 +592,9 @@ extension WebViewController: UIScrollViewDelegate { } @objc func scrollPositionDidChange() { + guard !isChangingSize else { + return + } let articleIDWhenAsked = article?.articleID webView?.evaluateJavaScript("recordScrollAnchor(); window.scrollY") { (scrollY, error) in guard error == nil else { @@ -728,6 +735,29 @@ private extension WebViewController { webView.loadHTMLString(html, baseURL: URL(string: rendering.baseURL)) } + /// Ignores mid-reflow offsets, then scrolls back to the reader’s place once the new width is in effect. + private func beginSizeChange(coordinator: UIViewControllerTransitionCoordinator?) { + guard !isChangingSize else { + return + } + isChangingSize = true + if let coordinator { + coordinator.animate(alongsideTransition: nil) { [weak self] _ in + self?.endSizeChange() + } + } else { + DispatchQueue.main.async { [weak self] in + self?.endSizeChange() + } + } + } + + private func endSizeChange() { + isChangingSize = false + restoreScrollAnchor() + scrollPositionDidChange() + } + /// Scrolls back to the element that was at the top of the viewport before the article /// reflowed, so rotating doesn't lose the reader's place. func restoreScrollAnchor() {