Last active
July 9, 2021 21:54
-
-
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`.
This file contains 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 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