UIScrollView Zooming & contentInset
<p>Simliar to iOS Photos App where the user is zooming in and out of an image by pinching:</p>

<p>UIView > UIScrollView > UIImageView > UIImage</p>

<p>Initially, I had the issue of zooming below scale 1: image being off centered.  I got it fixed by doing this:</p>

<pre><code>func scrollViewDidZoom(scrollView: UIScrollView) {
        let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
        let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
        scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
}
</code></pre>

<p>This works well when zooming out.</p>

<p>UIImage content mode is aspectFit</p>

<h2>Issue</h2>

<p>When I <strong>ZOOM IN</strong>, when zoomScale is above 1, scroll view insets need to hug the surroundings of the UIImage that the scroll view contains.  This takes away the dead-space that was surrounding the UIImage. IE, Photos app when zooming-in by pinching or double tapping. </p>

<h2>Tried</h2>

<pre><code>    func scrollViewDidZoom(scrollView: UIScrollView) {
    if scrollView.zoomScale &gt; 1 {
        let imageScale = (self.imageView.bounds.width/self.imageView.image!.size.width)
        let imageWidth = self.imageView.image!.size.width * imageScale
        let imageHeight = self.imageView.image!.size.height * imageScale
        scrollView.contentInset = UIEdgeInsetsMake(((scrollView.frame.height - imageHeight) * 0.5), (scrollView.frame.width - imageWidth) * 0.5 , 0, 0)
        print (scrollView.contentInset.top)
    }
    else {

        let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0)
        let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0)
        scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
    }
}
</code></pre>

<p>Above addition seems to vary the inset amount still.</p>

<h2>Update (images added)</h2>

<p>First image shows the default layout.  Rest shows when zoomed in.....</p>

<p><a href="https://i.stack.imgur.com/8reAF.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8reAF.png" alt="enter image description here"></a></p>