Put image view in navigation controller so that close and share buttons are laid out properly (ignoring iPad traffic lights). Fixes #5260.

This commit is contained in:
Brent Simmons committed 2026-05-27 11:38:09 -07:00
1 parent 8c02fb3ba4
commit 98b05ca283
4 files changed
+33 -50

No files matched your search

+12 -3
View File
@@ -52,8 +52,9 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning {
usingSpringWithDamping: 0.8,
initialSpringVelocity: 0.2,
animations: {
let imageController = transitionContext.viewController(forKey: .to) as! ImageViewController
imageView.frame = imageController.zoomedFrame
if let imageController = self.imageViewController(forKey: .to, in: transitionContext) {
imageView.frame = imageController.zoomedFrame
}
}, completion: { _ in
imageView.removeFromSuperview()
let toView = transitionContext.view(forKey: .to)!
@@ -63,7 +64,10 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning {
}
private func animateTransitionReturning(using transitionContext: UIViewControllerContextTransitioning) {
let imageController = transitionContext.viewController(forKey: .from) as! ImageViewController
guard let imageController = imageViewController(forKey: .from, in: transitionContext) else {
transitionContext.completeTransition(false)
return
}
let imageView = UIImageView(image: originImage)
imageView.frame = imageController.zoomedFrame
@@ -107,4 +111,9 @@ final class ImageTransition: NSObject, UIViewControllerAnimatedTransitioning {
})
}
private func imageViewController(forKey key: UITransitionContextViewControllerKey, in context: UIViewControllerContextTransitioning) -> ImageViewController? {
let navigationController = context.viewController(forKey: key) as? UINavigationController
return navigationController?.viewControllers.first as? ImageViewController
}
}
+17 -8
View File
@@ -9,14 +9,14 @@
import UIKit
final class ImageViewController: UIViewController {
@IBOutlet var closeButton: UIButton!
@IBOutlet var shareButton: UIButton!
@IBOutlet var imageScrollView: ImageScrollView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var titleBackground: UIVisualEffectView!
@IBOutlet var titleLeading: NSLayoutConstraint!
@IBOutlet var titleTrailing: NSLayoutConstraint!
private var shareButtonItem: UIBarButtonItem?
var image: UIImage!
var imageTitle: String?
var zoomedFrame: CGRect {
@@ -36,11 +36,19 @@ final class ImageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
closeButton.imageView?.contentMode = .scaleAspectFit
closeButton.accessibilityLabel = NSLocalizedString("Close", comment: "Close")
shareButton.accessibilityLabel = NSLocalizedString("Share", comment: "Share")
let closeButtonItem = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(done(_:)))
closeButtonItem.tintColor = Assets.Colors.primaryAccent
let shareButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(share(_:)))
shareButtonItem.tintColor = Assets.Colors.primaryAccent
navigationItem.leftBarButtonItem = closeButtonItem
navigationItem.rightBarButtonItem = shareButtonItem
self.shareButtonItem = shareButtonItem
imageScrollView.setup()
// The image viewer is full-screen, so the scroll view ignores
// the navigation bar and safe area insets. Otherwise the image is pushed down
// and doesn’t match the zoom transition’s target frame.
imageScrollView.contentInsetAdjustmentBehavior = .never
imageScrollView.imageScrollViewDelegate = self
imageScrollView.imageContentMode = .aspectFit
imageScrollView.initialOffset = .center
@@ -64,10 +72,11 @@ final class ImageViewController: UIViewController {
}
@IBAction func share(_ sender: Any) {
guard let image = image else { return }
guard let image else {
return
}
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = shareButton
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
activityViewController.popoverPresentationController?.barButtonItem = shareButtonItem
present(activityViewController, animated: true)
}