質問編集履歴
1
Pickerコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -64,7 +64,249 @@
|
|
64
64
|
|
65
65
|
```
|
66
66
|
|
67
|
-
|
67
|
+
###Pickerのコード
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
import Foundation
|
72
|
+
|
73
|
+
import UIKit
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
@objc
|
78
|
+
|
79
|
+
protocol PickerTextFieldDelegate: class {
|
80
|
+
|
81
|
+
@objc optional func pushDoneButton(pickerTextField: PickerTextField, text: String)
|
82
|
+
|
83
|
+
@objc optional func pushCancelButton(pickerTextField: PickerTextField)
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
class PickerTextField: UITextField, UIPickerViewDelegate, UIPickerViewDataSource {
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
weak var pickerTextFieldDelegate: PickerTextFieldDelegate?
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
private let pickerView = UIPickerView()
|
98
|
+
|
99
|
+
private let toolbarHeight: CGFloat = 44
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
var pickerDataArray = [String]() {
|
104
|
+
|
105
|
+
didSet {
|
106
|
+
|
107
|
+
if let selectText = pickerDataArray.first {
|
108
|
+
|
109
|
+
self.selectText = selectText
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
private var selectText: String = ""
|
120
|
+
|
121
|
+
var defaultText: String! {
|
122
|
+
|
123
|
+
get { return self.text }
|
124
|
+
|
125
|
+
set {
|
126
|
+
|
127
|
+
if let selectIndex = pickerDataArray.index(of: newValue) {
|
128
|
+
|
129
|
+
pickerView.selectRow(selectIndex, inComponent: 0, animated: false)
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
self.text = newValue
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
required init?(coder aDecoder: NSCoder) {
|
142
|
+
|
143
|
+
super.init(coder: aDecoder)
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
self.tintColor = UIColor.clear
|
148
|
+
|
149
|
+
pickerView.delegate = self
|
150
|
+
|
151
|
+
pickerView.dataSource = self
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
// 入力カーソル非表示
|
158
|
+
|
159
|
+
override func caretRect(for position: UITextPosition) -> CGRect {
|
160
|
+
|
161
|
+
return CGRect.zero
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
// 範囲選択カーソル非表示
|
166
|
+
|
167
|
+
override func selectionRects(for range: UITextRange) -> [Any] {
|
168
|
+
|
169
|
+
return []
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
// コピー・ペースト・選択等のメニュー非表示
|
176
|
+
|
177
|
+
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
178
|
+
|
179
|
+
return false
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
override var inputAccessoryView: UIView? {
|
186
|
+
|
187
|
+
get {
|
188
|
+
|
189
|
+
let toolBar = UIToolbar()
|
190
|
+
|
191
|
+
toolBar.barStyle = UIBarStyle.default
|
192
|
+
|
193
|
+
toolBar.isTranslucent = true
|
194
|
+
|
195
|
+
toolBar.tintColor = UIColor.black
|
196
|
+
|
197
|
+
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.pushDoneButton))
|
198
|
+
|
199
|
+
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.pushCancelButton))
|
200
|
+
|
201
|
+
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
|
202
|
+
|
203
|
+
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
|
204
|
+
|
205
|
+
toolBar.isUserInteractionEnabled = true
|
206
|
+
|
207
|
+
toolBar.sizeToFit()
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
return toolBar
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
set {}
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
override var inputView: UIView? {
|
222
|
+
|
223
|
+
get {
|
224
|
+
|
225
|
+
(self.value(forKeyPath: "textInputTraits") as AnyObject).setValue(UIColor.clear, forKey: "insertionPointColor")
|
226
|
+
|
227
|
+
self.tintColor = UIColor.clear
|
228
|
+
|
229
|
+
pickerView.frame = CGRect(x: 0,
|
230
|
+
|
231
|
+
y: toolbarHeight,
|
232
|
+
|
233
|
+
width: UIScreen.main.bounds.size.width,
|
234
|
+
|
235
|
+
height: pickerView.bounds.size.height)
|
236
|
+
|
237
|
+
pickerView.backgroundColor = UIColor.white
|
238
|
+
|
239
|
+
return pickerView
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
set {}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
// Done
|
250
|
+
|
251
|
+
@objc private func pushDoneButton() {
|
252
|
+
|
253
|
+
self.text = selectText
|
254
|
+
|
255
|
+
pickerTextFieldDelegate?.pushDoneButton?(pickerTextField: self, text: selectText)
|
256
|
+
|
257
|
+
resignFirstResponder()
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// Cancel
|
264
|
+
|
265
|
+
@objc private func pushCancelButton() {
|
266
|
+
|
267
|
+
pickerTextFieldDelegate?.pushCancelButton?(pickerTextField: self)
|
268
|
+
|
269
|
+
resignFirstResponder()
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
|
276
|
+
|
277
|
+
return pickerDataArray[row]
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
284
|
+
|
285
|
+
return 1
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
292
|
+
|
293
|
+
return pickerDataArray.count
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
|
300
|
+
|
301
|
+
selectText = pickerDataArray[row]
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
```
|
68
310
|
|
69
311
|
###試したこと
|
70
312
|
|