Last active
December 21, 2015 16:29
-
-
Save sugarmo/6334361 to your computer and use it in GitHub Desktop.
Center zooming view in scroll view -layoutSubview.
This file contains 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
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
// center the image as it becomes smaller than the size of the screen | |
CGSize boundsSize = self.bounds.size; | |
CGRect frameToCenter = self.imageView.frame; | |
// center horizontally | |
if (frameToCenter.size.width < boundsSize.width) { | |
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2; | |
} else { | |
frameToCenter.origin.x = 0; | |
} | |
// center vertically | |
if (frameToCenter.size.height < boundsSize.height) { | |
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2; | |
} else { | |
frameToCenter.origin.y = 0; | |
} | |
self.imageView.frame = frameToCenter; | |
// to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the | |
// tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens, | |
// which would cause the CATiledLayer to ask us for tiles of the wrong scales.) | |
self.imageView.contentScaleFactor = 1.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment