From e1a25ffa017d8f879a291a97bd4d112cdf0a776a Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sun, 25 Oct 2026 10:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=96=BC=EF=B8=8F=20Fix=20full-screen=20ima?= =?UTF-8?q?ge=20layout=20and=20resizing=20on=20iOS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lay out the destination before animating, recenter images after resizing, and calculate title margins without accumulating offsets. Give images the full screen, now that they have learnt to stay in the middle. --- iOS/Article/ImageScrollView.swift | 1 + iOS/Article/ImageTransition.swift | 12 +++++++++--- iOS/Article/ImageViewController.swift | 28 ++++++++++++++++----------- iOS/SceneCoordinator.swift | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/iOS/Article/ImageScrollView.swift b/iOS/Article/ImageScrollView.swift index 4415139b2..eecd273d9 100644 --- a/iOS/Article/ImageScrollView.swift +++ b/iOS/Article/ImageScrollView.swift @@ -298,6 +298,7 @@ open class ImageScrollView: UIScrollView { open func resize() { self.configureImageForSize(self.imageSize) + self.adjustFrameToCenter() } } diff --git a/iOS/Article/ImageTransition.swift b/iOS/Article/ImageTransition.swift index 2f80054fc..a7841f444 100644 --- a/iOS/Article/ImageTransition.swift +++ b/iOS/Article/ImageTransition.swift @@ -34,6 +34,14 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning { } private func animateTransitionPresenting(using transitionContext: UIViewControllerContextTransitioning) { + guard let toViewController = transitionContext.viewController(forKey: .to), + let toView = transitionContext.view(forKey: .to) else { + transitionContext.completeTransition(false) + return + } + + toView.frame = transitionContext.finalFrame(for: toViewController) + toView.layoutIfNeeded() let imageView = UIImageView(image: originImage) imageView.frame = originFrame @@ -56,9 +64,7 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning { } }, completion: { _ in imageView.removeFromSuperview() - if let toView = transitionContext.view(forKey: .to) { - transitionContext.containerView.addSubview(toView) - } + transitionContext.containerView.addSubview(toView) transitionContext.completeTransition(true) }) } diff --git a/iOS/Article/ImageViewController.swift b/iOS/Article/ImageViewController.swift index ec0ba7800..b0f2d8c9c 100644 --- a/iOS/Article/ImageViewController.swift +++ b/iOS/Article/ImageViewController.swift @@ -16,6 +16,8 @@ final class ImageViewController: UIViewController { @IBOutlet var titleTrailing: NSLayoutConstraint! private var shareButtonItem: UIBarButtonItem? + private var initialTitleConstraintConstants: (leading: CGFloat, trailing: CGFloat)? + private var lastImageScrollViewSize = CGSize.zero var image: UIImage! var imageTitle: String? @@ -59,7 +61,7 @@ final class ImageViewController: UIViewController { imageScrollView.display(image: image) titleLabel.text = imageTitle ?? "" - layoutTitleLabel() + initialTitleConstraintConstants = (titleLeading.constant, titleTrailing.constant) guard imageTitle != "" else { titleBackground.removeFromSuperview() @@ -68,11 +70,15 @@ final class ImageViewController: UIViewController { titleBackground.layer.cornerRadius = 6 } - override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { - super.viewWillTransition(to: size, with: coordinator) - coordinator.animate(alongsideTransition: { [weak self] _ in - self?.imageScrollView.resize() - }) + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + layoutTitleLabel() + + let imageScrollViewSize = imageScrollView.bounds.size + if imageScrollViewSize != lastImageScrollViewSize { + lastImageScrollViewSize = imageScrollViewSize + imageScrollView.resize() + } } @IBAction func share(_ sender: Any) { @@ -89,11 +95,11 @@ final class ImageViewController: UIViewController { } private func layoutTitleLabel() { - let width = view.frame.width - let multiplier = traitCollection.userInterfaceIdiom == .pad ? CGFloat(0.1) : CGFloat(0.04) - titleLeading.constant += width * multiplier - titleTrailing.constant -= width * multiplier - titleLabel.layoutIfNeeded() + guard let initialTitleConstraintConstants else { return } + let width = view.bounds.width + let multiplier = traitCollection.horizontalSizeClass == .regular ? CGFloat(0.1) : CGFloat(0.04) + titleLeading.constant = initialTitleConstraintConstants.leading + width * multiplier + titleTrailing.constant = initialTitleConstraintConstants.trailing - width * multiplier } } diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 261975ac6..908537a1c 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -1647,7 +1647,7 @@ struct SidebarItemNode: Hashable, Sendable { imageVC.imageTitle = imageTitle imageVC.transition = transition let navController = UINavigationController(rootViewController: imageVC) - navController.modalPresentationStyle = .currentContext + navController.modalPresentationStyle = .fullScreen navController.transitioningDelegate = transition rootSplitViewController.present(navController, animated: true) }