Created
February 4, 2020 20:55
-
-
Save GabrielSilveiraa/0f560d862cf807db7801d8ca98a90005 to your computer and use it in GitHub Desktop.
A custom UIView class designed to use with view coded layout
This file contains 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 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