Skip to content

Instantly share code, notes, and snippets.

@sourleangchhean168
Last active May 2, 2022 07:16
Show Gist options
  • Save sourleangchhean168/f41a70e4d284c39da931f92c8f6c6213 to your computer and use it in GitHub Desktop.
Save sourleangchhean168/f41a70e4d284c39da931f92c8f6c6213 to your computer and use it in GitHub Desktop.
Aspect Ratio in Swift
//iOS 13, swift 5.1
//Example programmatic constraints with aspect ratio.
imageView.translatesAutoresizingMaskIntoConstraints = false
//A) 4:3
imageView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 3.0/4.0).isActive = true
//B) 16:9
imageView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 9.0/16.0).isActive = true
//Ref: https://stackoverflow.com/questions/52805754/image-view-169-aspect-ratio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment