Skip to content

Instantly share code, notes, and snippets.

@ConorBrady
Last active April 26, 2017 14:57
Show Gist options
  • Save ConorBrady/ebfc3ac799c2c7e1891b12053aaef23a to your computer and use it in GitHub Desktop.
Save ConorBrady/ebfc3ac799c2c7e1891b12053aaef23a to your computer and use it in GitHub Desktop.
protocol HasModel {
associatedtype Model
var model: Model? { get set }
}
protocol HasHeight {
static var height: CGFloat { get }
}
extension UIView {
func enableConstraints() -> Self {
return self <== {
$0.translatesAutoresizingMaskIntoConstraints = false
}
}
func layout(center child: UIView) -> UIView {
child.enableConstraints()
addSubview(child)
NSLayoutConstraint.activate([
centerXAnchor.constraint(equalTo: child.centerXAnchor),
centerYAnchor.constraint(equalTo: child.centerYAnchor)
])
return self
}
func layout(width: CGFloat, height: CGFloat) -> UIView {
enableConstraints()
NSLayoutConstraint.activate([
widthAnchor.constraint(equalToConstant: width),
heightAnchor.constraint(equalToConstant: height)
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment