Skip to content

Instantly share code, notes, and snippets.

@im-jersh
Last active July 9, 2022 01:22
Show Gist options
  • Select an option

  • Save im-jersh/9bba646fc2cbef56b75a1f1e086a24cf to your computer and use it in GitHub Desktop.

Select an option

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
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