Long story short, I'm trying to build functionality similar to Photos.app.
I have a UIScrollView with a UIImageView inside of it set up in the Storyboard. Zooming works, but I'm having trouble keeping it centered. In all my frame based scroll view implementations I've centered it as follows, which works beautifully:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGRect newImageViewFrame = self.imageView.frame;
// Center horizontally
if (newImageViewFrame.size.width < CGRectGetWidth(scrollView.bounds)) {
newImageViewFrame.origin.x = (CGRectGetWidth(scrollView.bounds) - CGRectGetWidth(self.imageView.frame)) / 2;
}
else {
newImageViewFrame.origin.x = 0;
}
// Center vertically
if (newImageViewFrame.size.height < CGRectGetHeight(scrollView.bounds)) {
newImageViewFrame.origin.y = (CGRectGetHeight(scrollView.bounds) - CGRectGetHeight(self.imageView.frame)) / 2;
}
else {
newImageViewFrame.origin.y = 0;
}
self.imageView.frame = newImageViewFrame;
}
But with Auto Layout it isn't at all.
I have no constraints on the UIScrollView or UIImageView, as I don't know what they should be. I'm thinking I should glue the UIScrollView to the four corners, but for the UIImageView I'm not completely sure, as the zooming changes its frame.
Here's an example project: http://cl.ly/21371H3q381N
How would I go about having zooming with centering work with Auto Layout?