Created
April 30, 2020 13:15
-
-
Save henrik-dmg/33de4c85334e445dfff538b7ae1db568 to your computer and use it in GitHub Desktop.
Fits the calling size into the provided while retaining aspec ratio
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
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