Skip to content

Instantly share code, notes, and snippets.

@zmcartor
Created September 9, 2014 17:26
Show Gist options
  • Save zmcartor/9f203d4f15f6310af449 to your computer and use it in GitHub Desktop.
Save zmcartor/9f203d4f15f6310af449 to your computer and use it in GitHub Desktop.
Center ScrollView Contents
// Can also be placed within scrollviews layoutSubviews
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment