-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginView.swift
More file actions
162 lines (146 loc) · 6.78 KB
/
Copy pathLoginView.swift
File metadata and controls
162 lines (146 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// LoginView.swift
// PowerHome
//
// Created by Francisco Javier Chacon de Dios on 10/02/18.
// Copyright © 2018 Francisco Javier Chacon de Dios. All rights reserved.
//
import UIKit
/// Login view to be used in
class LoginView: BaseView {
lazy var usernameLabel: UILabel = {
let label: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
label.text = "Username"
label.textColor = .darkGray
label.font = UIFont.systemFont(ofSize: 14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var passwordLabel: UILabel = {
let label: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
label.text = "Password"
label.textColor = .darkGray
label.font = UIFont.systemFont(ofSize: 14)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var usernameTextField: UITextField = {
let textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
textField.placeholder = "user@example.com"
textField.autocorrectionType = .no
textField.keyboardType = .emailAddress
textField.autocapitalizationType = .none
textField.borderStyle = .roundedRect
textField.translatesAutoresizingMaskIntoConstraints = false
textField.delegate = self
return textField
}()
lazy var passwordTextField: UITextField = {
let textField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.borderStyle = .roundedRect
textField.isSecureTextEntry = true
textField.translatesAutoresizingMaskIntoConstraints = false
textField.delegate = self
return textField
}()
lazy var loginButton: UISquareButton = {
let button: UISquareButton = UISquareButton(buttonType: .system)
button.setTitle("Login", for: .normal)
return button
}()
lazy var signUpButton: UISquareButton = {
let button: UISquareButton = UISquareButton(buttonType: .system)
button.setTitle("Sign Up", for: .normal)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
addSubview(usernameLabel)
addSubview(usernameTextField)
addSubview(passwordLabel)
addSubview(passwordTextField)
addSubview(loginButton)
addSubview(signUpButton)
let dismissKeyboardTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard(_:)))
addGestureRecognizer(dismissKeyboardTapGestureRecognizer)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
// MARK: - AutoLayout
extension LoginView {
override func updateConstraints() {
super.updateConstraints()
let usernameLabelHorizontalConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-15-[usernameLabel]-15-|",
options: .alignAllBottom,
metrics: nil,
views: ["usernameLabel": usernameLabel])
let verticalElementsConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-30-[usernameLabel]-15-[usernameTextField]-30-[passwordLabel]-15-[passwordTextField]-40-[loginButton]-15-[signUpButton]",
options: .alignAllLeading,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"usernameTextField": usernameTextField,
"passwordLabel": passwordLabel,
"passwordTextField": passwordTextField,
"loginButton": loginButton,
"signUpButton": signUpButton
])
let usernameTextFieldEqualWidthsToUsernameLabelConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "[usernameTextField(==usernameLabel)]",
options: .alignAllCenterX,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"usernameTextField": usernameTextField
])
let passwordLabelEqualWidthsToUsernameLabelConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "[passwordLabel(==usernameLabel)]",
options: .alignAllCenterX,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"passwordLabel": passwordLabel
])
let passwordTextFieldTextFieldEqualWidthsToUsernameLabelConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "[passwordTextField(==usernameLabel)]",
options: .alignAllCenterX,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"passwordTextField": passwordTextField
])
let loginButtonEqualWidthsToUsernameLabelConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "[loginButton(==usernameLabel)]",
options: .alignAllCenterX,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"loginButton": loginButton
])
let signUpButtonEqualWidthsToUsernameLabelConstraints: [NSLayoutConstraint] = NSLayoutConstraint.constraints(
withVisualFormat: "[signUpButton(==usernameLabel)]",
options: .alignAllCenterX,
metrics: nil,
views: [
"usernameLabel": usernameLabel,
"signUpButton": signUpButton
])
let loginButtonHeightConstraint: NSLayoutConstraint = NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)
let signUpButtonHeighConstraint: NSLayoutConstraint = NSLayoutConstraint(item: signUpButton, attribute: .height, relatedBy: .equal, toItem: loginButton, attribute: .height, multiplier: 1, constant: 0)
addConstraints(usernameLabelHorizontalConstraints)
addConstraints(verticalElementsConstraints)
addConstraints(usernameTextFieldEqualWidthsToUsernameLabelConstraints)
addConstraints(passwordLabelEqualWidthsToUsernameLabelConstraints)
addConstraints(passwordTextFieldTextFieldEqualWidthsToUsernameLabelConstraints)
addConstraints(loginButtonEqualWidthsToUsernameLabelConstraints)
addConstraints(signUpButtonEqualWidthsToUsernameLabelConstraints)
addConstraints([loginButtonHeightConstraint, signUpButtonHeighConstraint])
}
}