Created
September 9, 2014 17:26
-
-
Save zmcartor/9f203d4f15f6310af449 to your computer and use it in GitHub Desktop.
Center ScrollView Contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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