Created
February 14, 2023 07:28
-
-
Save ArtiomKha/b12998dcaf4718dbb7253b79cf3c9b29 to your computer and use it in GitHub Desktop.
Source code for the Medium article about separating view and view controller
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 | |
class GenericViewController<T: UIView>: UIViewController { | |
public var rootView: T { return view as! T } | |
override open func loadView() { | |
self.view = T() | |
} | |
} |
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 | |
class MainView: UIView { | |
let usernameHintLabel: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.text = "Username" | |
label.font = .systemFont(ofSize: 14, weight: .semibold) | |
label.textAlignment = .left | |
return label | |
}() | |
let usernameTextField: UITextField = { | |
let textfield = UITextField() | |
textfield.translatesAutoresizingMaskIntoConstraints = false | |
textfield.borderStyle = .roundedRect | |
textfield.placeholder = "Username" | |
textfield.font = .systemFont(ofSize: 18, weight: .regular) | |
return textfield | |
}() | |
let passwordHintLabel: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.text = "Password" | |
label.font = .systemFont(ofSize: 14, weight: .semibold) | |
label.textAlignment = .left | |
return label | |
}() | |
let passwordTextField: UITextField = { | |
let textfield = UITextField() | |
textfield.translatesAutoresizingMaskIntoConstraints = false | |
textfield.borderStyle = .roundedRect | |
textfield.placeholder = "Password" | |
textfield.font = .systemFont(ofSize: 18, weight: .regular) | |
return textfield | |
}() | |
let submitButton: UIButton = { | |
let button = UIButton() | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.setTitle("Sign In", for: .normal) | |
button.setTitleColor(.white, for: .normal) | |
button.backgroundColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1) | |
button.layer.cornerRadius = 8 | |
return button | |
}() | |
let stackView: UIStackView = { | |
let stackView = UIStackView() | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
stackView.alignment = .fill | |
stackView.distribution = .fill | |
stackView.axis = .vertical | |
stackView.spacing = 8 | |
return stackView | |
}() | |
init() { | |
super.init(frame: .zero) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
setup() | |
} | |
func setup() { | |
backgroundColor = .white | |
addSubview(stackView) | |
addSubview(submitButton) | |
[usernameHintLabel, usernameTextField, passwordHintLabel, passwordTextField].forEach { stackView.addArrangedSubview($0) } | |
stackView.setCustomSpacing(16, after: usernameTextField) | |
NSLayoutConstraint.activate([ | |
stackView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 48), | |
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16), | |
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16), | |
submitButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16), | |
submitButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16), | |
submitButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -32), | |
submitButton.heightAnchor.constraint(equalToConstant: 56) | |
]) | |
} | |
} |
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 | |
class MainViewController: GenericViewController<MainView> { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
func setupTextFields() { | |
rootView.usernameTextField.delegate = self | |
rootView.passwordTextField.delegate = self | |
} | |
func setupButton() { | |
rootView.submitButton.addTarget(self, action: #selector(buttonPressed), for: .primaryActionTriggered) | |
} | |
@objc func buttonPressed() { | |
} | |
} | |
extension MainViewController: UITextFieldDelegate { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment