Skip to content

Instantly share code, notes, and snippets.

@bielikb
Last active September 13, 2019 23:51
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 bielikb/85b6211be1258b1bf9781eed8ddfbbad to your computer and use it in GitHub Desktop.
Save bielikb/85b6211be1258b1bf9781eed8ddfbbad to your computer and use it in GitHub Desktop.
Use Autolayout
@propertyWrapper
public struct UseAutoLayout<T: UIView> {
var value: T
public var wrappedValue: T {
get { return value }
set { self.value.translatesAutoresizingMaskIntoConstraints = false }
}
public init(wrappedValue: T) {
value = wrappedValue
}
}
final class ViewController: UIViewController {
@UseAutoLayout var label = UILabel()
override func viewDidLoad() {
label = UILabel()
label.backgroundColor = .yellow
label.textAlignment = .center
self.view.addSubview(label)
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
label.text = "autolayout"
print("translatesAutoresizingMaskIntoConstraints: \(view.translatesAutoresizingMaskIntoConstraints)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment