mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-01 09:50:16 +00:00
🖼️ 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:
4 files changed
+28
-15
No files matched your search
@@ -298,6 +298,7 @@ open class ImageScrollView: UIScrollView {
|
||||
|
||||
open func resize() {
|
||||
self.configureImageForSize(self.imageSize)
|
||||
self.adjustFrameToCenter()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user