Skip to content

Instantly share code, notes, and snippets.

@sauravexodus
Created September 14, 2017 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauravexodus/1c127677a3c289a03ac6d541e9800a67 to your computer and use it in GitHub Desktop.
Save sauravexodus/1c127677a3c289a03ac6d541e9800a67 to your computer and use it in GitHub Desktop.
Relative Dimensioning in Swift Manual Layouts
extension CGFloat {
/**
The relative dimension to the corresponding screen size.
//Usage
let someView = UIView(frame: CGRect(x: 0, y: 0, width: 320.dp, height: 40.dp)
**Warning** Only works with size references from @1x mockups.
*/
var dp: CGFloat {
return (self / 320) * UIScreen.main.bounds.width
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment