Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- let mainView = MainView()
- view.addSubview(mainView.root)
- mainView.buildConstraints()
- view.leadingAnchor.constraint(equalTo: mainView.root.leadingAnchor).isActive = true
- }
- }
- struct BuildableView<T> where T: UIView {
- var view: T
- init(_ build: (_ initialView: T) -> T) {
- self.view = build(T())
- }
- init(_ build: () -> T) {
- self.view = build()
- }
- }
- struct MainView {
- private let label = BuildableView<UILabel> { label in
- label.text = "hey"
- label.textColor = .blue
- return label
- }
- private let button = BuildableView<UIButton> { button in
- button.setTitle("button title", for: .normal)
- button.backgroundColor = .cyan
- return button
- }
- let root = UIView()
- init() {
- root.addSubview(label.view)
- root.addSubview(button.view)
- }
- func buildConstraints() {
- label.view.trailingAnchor.constraint(equalTo: button.view.leadingAnchor).isActive = true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement