Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import UIKit
  2. import Cartography
  3.  
  4. class LoginView: UIView {
  5.  
  6. var tapLoginButton: ((_ username: String, _ password: String) -> ())?
  7.  
  8. let userNameTextField = { () -> UITextField in
  9. let textField = UITextField()
  10. textField.placeholder = "Username"
  11. textField.accessibilityLabel = "Username"
  12. textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
  13. textField.autocapitalizationType = .none
  14. return textField
  15. }()
  16.  
  17. let passwordTextField = { () -> UITextField in
  18. let textField = UITextField()
  19. textField.placeholder = "Password"
  20. textField.accessibilityLabel = "Password"
  21. textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
  22. textField.autocapitalizationType = .none
  23. textField.isSecureTextEntry = true
  24. return textField
  25. }()
  26.  
  27. let loginButton = { () -> UIButton in
  28. let button = UIButton()
  29. button.accessibilityLabel = "Login"
  30. button.setTitle("Login", for: .normal)
  31. button.setTitleColor(.gray, for: .normal)
  32. button.isHidden = true
  33. return button
  34. }()
  35.  
  36. init() {
  37. super.init(frame: CGRect.zero)
  38. build()
  39. }
  40.  
  41. required init?(coder aDecoder: NSCoder) {
  42. fatalError("init(coder:) has not been implemented")
  43. }
  44.  
  45. func build() {
  46. buildViewHierarchy()
  47. buildConstrains()
  48. setup()
  49. }
  50.  
  51. func buildViewHierarchy() {
  52. addSubview(userNameTextField)
  53. addSubview(passwordTextField)
  54. addSubview(loginButton)
  55. }
  56.  
  57. func buildConstrains() {
  58.  
  59. let verticalMargin: CGFloat = 16.0
  60. let horizontalMargin: CGFloat = 8.0
  61.  
  62. constrain(self, userNameTextField, passwordTextField, loginButton) { view, username, password, login in
  63.  
  64. username.top == view.top + verticalMargin
  65. username.left == view.left + horizontalMargin
  66. username.right == view.right - horizontalMargin
  67.  
  68. password.top == username.bottom + verticalMargin
  69. password.left == username.left
  70. password.right == username.right
  71.  
  72. login.top == password.bottom + verticalMargin
  73. login.left == password.left
  74. login.right == password.right
  75. login.bottom == view.bottom - verticalMargin
  76. }
  77. }
  78.  
  79. func setup() {
  80. self.loginButton.addTarget(self, action: #selector(tapLogin), for: .touchUpInside)
  81. }
  82.  
  83. func tapLogin() {
  84.  
  85. guard let username = userNameTextField.text, let password = passwordTextField.text else {
  86. return
  87. }
  88.  
  89. self.tapLoginButton?(username, password)
  90. }
  91.  
  92. func textFieldDidChange(_ textField: UITextField) {
  93. if self.userNameTextField.isEmpty() || self.passwordTextField.isEmpty() {
  94. self.loginButton.isHidden = true
  95. } else {
  96. self.loginButton.isHidden = false
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement