Skip to content

Instantly share code, notes, and snippets.

@henrik-dmg
Created April 30, 2020 13:15
Show Gist options
  • Save henrik-dmg/33de4c85334e445dfff538b7ae1db568 to your computer and use it in GitHub Desktop.
Save henrik-dmg/33de4c85334e445dfff538b7ae1db568 to your computer and use it in GitHub Desktop.
Fits the calling size into the provided while retaining aspec ratio
extension CGSize {
func sizeFittingPreservingAspectRatio(_ otherSize: CGSize) -> CGSize {
if height <= width {
let scaledWidth = (otherSize.height / height) * width
return CGSize(width: scaledWidth, height: otherSize.height)
} else {
let scaledHeight = (otherSize.width / width) * height
return CGSize(width: otherSize.width, height: scaledHeight)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment