🖼️ Fix full-screen image layout and resizing on iOS

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.
This commit is contained in:
Stuart Breckenridge committed 2026-10-25 10:41:37 +08:00
1 parent ae211b90c9
commit e1a25ffa01
4 files changed
+28 -15

No files matched your search

+1
View File
@@ -298,6 +298,7 @@ open class ImageScrollView: UIScrollView {
open func resize() {
self.configureImageForSize(self.imageSize)
self.adjustFrameToCenter()
}
}
+9 -3
View File
@@ -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)
})
}
+17 -11
View File
@@ -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
}
}
+1 -1
View File
@@ -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)
}