Skip to content

Instantly share code, notes, and snippets.

@GabrielSilveiraa
Created February 4, 2020 20:55
Show Gist options
  • Save GabrielSilveiraa/0f560d862cf807db7801d8ca98a90005 to your computer and use it in GitHub Desktop.
Save GabrielSilveiraa/0f560d862cf807db7801d8ca98a90005 to your computer and use it in GitHub Desktop.
A custom UIView class designed to use with view coded layout
import UIKit
/**
**A Custom UIView class designed to use with view coded layout**
**Lifecycle:**
1. `init()`
2. `initialize()` *Add subviews*
3. `installConstraints()` *Setup constraints*
*/
typealias BaseView = BaseViewClass & BaseViewProtocol
protocol BaseViewProtocol {
func setupConstraints()
func initialize()
}
class BaseViewClass: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init() {
super.init(frame: CGRect.zero)
self.setup()
}
private func setup() {
guard let self = self as? BaseView else {
fatalError("Use BaseView instead of BaseViewClass")
}
self.initialize()
self.setupConstraints()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment