Skip to content

Instantly share code, notes, and snippets.

@marlonjames71
Last active July 9, 2021 21:54
Show Gist options
  • Save marlonjames71/fc508cddcc0be12253233f990a8b3ba5 to your computer and use it in GitHub Desktop.
Save marlonjames71/fc508cddcc0be12253233f990a8b3ba5 to your computer and use it in GitHub Desktop.
A little helper method when doing programmatic auto layout. No more typing out `translatesAutoresizingMaskIntoConstraints`.
extension UIView {
/// Sets `translatesAutoresizingMaskIntoConstraints` to false.
///
/// // Example Usage:
///
/// private lazy var captionLabel: UILabel = {
/// let label = UILabel().forAutoLayout()
/// label.text = viewModel.captionText
/// label.textAlignment = .center
/// return label
/// }
///
/// - Returns: `Self` for the convenience of inline chaining.
///
func forAutoLayout() -> Self {
self.translatesAutoresizingMaskIntoConstraints = false
return self
}
}
// This was discovered in issue: #1 via this newsletter ⟶ https://newsletter.ioscodereview.com
// Credit goes to Marcus Ziadé with this tweet ⟶ https://twitter.com/ZiadeMarcus/status/1255509639015383044?s=20
// I wanted to make a gist with this code so I'd always have it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment