Last active
July 9, 2022 01:22
-
-
Save im-jersh/9bba646fc2cbef56b75a1f1e086a24cf to your computer and use it in GitHub Desktop.
A Swift property wrapper to make `translatesAutoresizingMaskIntoConstraints = false` a thing of the past
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
| import UIKit | |
| /// A property wrapper that ensures the wrapped `UIView`'s | |
| /// `translatesAutoresizingMaskIntoConstraints` property is | |
| /// set to `false` | |
| @propertyWrapper | |
| public struct AutoLayout<T: UIView> { | |
| public var wrappedValue: T { | |
| didSet { | |
| wrappedValue.translatesAutoresizingMaskIntoConstraints = false | |
| } | |
| } | |
| public init(wrappedValue: T) { | |
| wrappedValue.translatesAutoresizingMaskIntoConstraints = false | |
| self.wrappedValue = wrappedValue | |
| } | |
| } | |
| // Example | |
| class ViewController: UIViewController { | |
| @AutoLayout private var label = UILabel() | |
| @AutoLayout private var button = UIButton() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment