-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathFormView.swift
108 lines (85 loc) · 4.16 KB
/
FormView.swift
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
// Copyright (c) 2017 Luc Dion
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
import PinLayout
class FormView: BaseFormView {
private let formContainerView = UIView()
private let formTitleLabel = UILabel()
private let nameField = UITextField()
private let ageSwitch = UISwitch()
private let ageField = UITextField()
private let addressField = UITextField()
override init() {
super.init()
backgroundColor = .white
formContainerView.backgroundColor = UIColor.pinLayoutColor.withAlphaComponent(0.3)
formContainerView.layer.cornerRadius = 5
formScrollView.addSubview(formContainerView)
formTitleLabel.text = "Simple Form Example"
formTitleLabel.font = .boldSystemFont(ofSize: 14)
formTitleLabel.sizeToFit()
formContainerView.addSubview(formTitleLabel)
nameField.placeholder = "Name"
nameField.layer.borderColor = UIColor.gray.cgColor
nameField.layer.borderWidth = 1
formContainerView.addSubview(nameField)
ageSwitch.tintColor = .lightGray
ageSwitch.addTarget(self, action: #selector(didChangeAgeSwitch), for: .valueChanged)
formContainerView.addSubview(ageSwitch)
ageField.placeholder = "Age"
ageField.alpha = 0
ageField.layer.borderColor = UIColor.gray.cgColor
ageField.layer.borderWidth = 1
formContainerView.addSubview(ageField)
addressField.placeholder = "Address"
addressField.layer.borderColor = UIColor.gray.cgColor
addressField.layer.borderWidth = 1
formContainerView.addSubview(addressField)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
layoutFormFields()
}
private func layoutFormFields() {
let margin: CGFloat = 12
// Layout the formContainerView using the view's safeArea.
formContainerView.pin.top().hCenter().width(100%).maxWidth(400).pinEdges().margin(margin)
formTitleLabel.pin.topCenter(margin)
nameField.pin.below(of: formTitleLabel).horizontally().height(40).margin(margin)
ageSwitch.pin.below(of: nameField, aligned: .left).marginTop(margin).sizeToFit()
ageField.pin.below(of: ageSwitch).horizontally().height(40).margin(margin)
// Layout the Address UITextField below the last visible view, either ageSwitch or ageField.
addressField.pin.below(of: visible([ageSwitch, ageField])).horizontally().height(40).margin(margin)
// Adjust the form container bottom to contains all its childrens
formContainerView.pin.wrapContent(.vertically, padding: margin)
// Adjust UIScrollView contentSize
formScrollView.contentSize = formContainerView.frame.size
}
@objc
internal func didChangeAgeSwitch(uiSwitch: UISwitch) {
// Animate the appearance/disapearance of the age UITextField
UIView.animate(withDuration: 0.2) {
self.ageField.alpha = uiSwitch.isOn ? 1 : 0
self.layoutFormFields()
}
}
}