Handle safe areas for image preview
This commit is contained in:
parent
e2e93bdaf0
commit
7ab62394de
|
@ -112,23 +112,14 @@ extension MediaPreviewImageView {
|
||||||
// reset to normal
|
// reset to normal
|
||||||
zoomScale = minimumZoomScale
|
zoomScale = minimumZoomScale
|
||||||
|
|
||||||
let imageViewSize = AVMakeRect(aspectRatio: image.size, insideRect: container.bounds).size
|
let imageViewSize = AVMakeRect(aspectRatio: image.size, insideRect: container.bounds.inset(by: container.safeAreaInsets)).size
|
||||||
let imageContentInset: UIEdgeInsets = {
|
|
||||||
if imageViewSize.width == container.bounds.width {
|
|
||||||
return UIEdgeInsets(top: 0.5 * (container.bounds.height - imageViewSize.height), left: 0, bottom: 0, right: 0)
|
|
||||||
} else {
|
|
||||||
return UIEdgeInsets(top: 0, left: 0.5 * (container.bounds.width - imageViewSize.width), bottom: 0, right: 0)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
imageView.frame = CGRect(origin: .zero, size: imageViewSize)
|
imageView.frame = CGRect(origin: .zero, size: imageViewSize)
|
||||||
if imageView.image == nil {
|
if imageView.image == nil {
|
||||||
imageView.image = image
|
imageView.image = image
|
||||||
}
|
}
|
||||||
contentSize = imageViewSize
|
contentSize = imageViewSize
|
||||||
contentInset = imageContentInset
|
|
||||||
|
|
||||||
centerScrollViewContents()
|
centerScrollViewContents()
|
||||||
contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
|
|
||||||
|
|
||||||
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup image for container %s", ((#file as NSString).lastPathComponent), #line, #function, container.frame.debugDescription)
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: setup image for container %s", ((#file as NSString).lastPathComponent), #line, #function, container.frame.debugDescription)
|
||||||
}
|
}
|
||||||
|
@ -192,10 +183,7 @@ extension MediaPreviewImageView {
|
||||||
frame.size = realImageSize
|
frame.size = realImageSize
|
||||||
imageView.frame = frame
|
imageView.frame = frame
|
||||||
|
|
||||||
let screenSize = self.frame.size
|
contentInset = self.safeAreaInsets
|
||||||
let offsetX = screenSize.width > realImageSize.width ? (screenSize.width - realImageSize.width) / 2 : 0
|
|
||||||
let offsetY = screenSize.height > realImageSize.height ? (screenSize.height - realImageSize.height) / 2 : 0
|
|
||||||
contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX)
|
|
||||||
|
|
||||||
// The scroll view has zoomed, so you need to re-center the contents
|
// The scroll view has zoomed, so you need to re-center the contents
|
||||||
let scrollViewSize = scrollViewVisibleSize
|
let scrollViewSize = scrollViewVisibleSize
|
||||||
|
|
|
@ -46,10 +46,10 @@ extension MediaPreviewImageViewController {
|
||||||
previewImageView.translatesAutoresizingMaskIntoConstraints = false
|
previewImageView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
view.addSubview(previewImageView)
|
view.addSubview(previewImageView)
|
||||||
NSLayoutConstraint.activate([
|
NSLayoutConstraint.activate([
|
||||||
previewImageView.frameLayoutGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
|
previewImageView.frameLayoutGuide.topAnchor.constraint(equalTo: view.topAnchor),
|
||||||
previewImageView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
previewImageView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||||
previewImageView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
previewImageView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||||
previewImageView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
|
previewImageView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
||||||
])
|
])
|
||||||
|
|
||||||
tapGestureRecognizer.addTarget(self, action: #selector(MediaPreviewImageViewController.tapGestureRecognizerHandler(_:)))
|
tapGestureRecognizer.addTarget(self, action: #selector(MediaPreviewImageViewController.tapGestureRecognizerHandler(_:)))
|
||||||
|
|
Loading…
Reference in New Issue