Advertisement
Guest User

Untitled

a guest
Aug 24th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.15 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.     override func viewDidLoad() {
  3.         super.viewDidLoad()
  4.         let mainView = MainView()
  5.        
  6.         view.addSubview(mainView.root)
  7.         mainView.buildConstraints()
  8.         view.leadingAnchor.constraint(equalTo: mainView.root.leadingAnchor).isActive = true
  9.     }
  10. }
  11.  
  12. struct BuildableView<T> where T: UIView {
  13.     var view: T
  14.     init(_ build: (_ initialView: T) -> T) {
  15.         self.view = build(T())
  16.     }
  17.     init(_ build: () -> T) {
  18.         self.view = build()
  19.     }
  20. }
  21.  
  22. struct MainView {
  23.     private let label = BuildableView<UILabel> { label in
  24.         label.text = "hey"
  25.         label.textColor = .blue
  26.         return label
  27.     }
  28.     private let button = BuildableView<UIButton> { button in
  29.         button.setTitle("button title", for: .normal)
  30.         button.backgroundColor = .cyan
  31.         return button
  32.     }
  33.     let root = UIView()
  34.    
  35.     init() {
  36.         root.addSubview(label.view)
  37.         root.addSubview(button.view)
  38.     }
  39.    
  40.     func buildConstraints() {
  41.         label.view.trailingAnchor.constraint(equalTo: button.view.leadingAnchor).isActive = true
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement