@@ -58,7 +58,7 @@ open class DropDown : UITextField{
58
58
fileprivate var parentController : UIViewController ?
59
59
fileprivate var pointToParent = CGPoint ( x: 0 , y: 0 )
60
60
fileprivate var backgroundView = UIView ( )
61
-
61
+ fileprivate var keyboardHeight : CGFloat = 0
62
62
63
63
public var optionArray = [ String] ( ) {
64
64
didSet{
@@ -91,6 +91,21 @@ open class DropDown : UITextField{
91
91
arrow. frame = CGRect ( x: center. x - arrowSize/ 2 , y: center. y - arrowSize/ 2 , width: arrowSize, height: arrowSize)
92
92
}
93
93
}
94
+ @IBInspectable public var arrowColor : UIColor = . black {
95
+ didSet{
96
+ arrow. arrowColor = arrowColor
97
+ }
98
+ }
99
+ @IBInspectable public var checkMarkEnabled : Bool = true {
100
+ didSet{
101
+
102
+ }
103
+ }
104
+ @IBInspectable public var handleKeyboard : Bool = true {
105
+ didSet{
106
+
107
+ }
108
+ }
94
109
95
110
// Init
96
111
public override init ( frame: CGRect ) {
@@ -121,13 +136,38 @@ open class DropDown : UITextField{
121
136
let arrowContainerView = UIView ( frame: rightView. frame)
122
137
self . rightView? . addSubview ( arrowContainerView)
123
138
let center = arrowContainerView. center
124
- arrow = Arrow ( origin: CGPoint ( x: center. x - arrowSize/ 2 , y: center. y - arrowSize/ 2 ) , size: arrowSize)
139
+ arrow = Arrow ( origin: CGPoint ( x: center. x - arrowSize/ 2 , y: center. y - arrowSize/ 2 ) , size: arrowSize )
125
140
arrowContainerView. addSubview ( arrow)
126
141
127
142
self . backgroundView = UIView ( frame: . zero)
128
143
self . backgroundView. backgroundColor = . clear
129
144
addGesture ( )
145
+ if isSearchEnable && handleKeyboard{
146
+ NotificationCenter . default. addObserver ( forName: NSNotification . Name. UIKeyboardWillShow, object: nil , queue: nil ) { ( notification) in
147
+ if self . isFirstResponder{
148
+ let userInfo : NSDictionary = notification. userInfo! as NSDictionary
149
+ let keyboardFrame : NSValue = userInfo [ UIKeyboardFrameEndUserInfoKey] as! NSValue
150
+ let keyboardRectangle = keyboardFrame. cgRectValue
151
+ self . keyboardHeight = keyboardRectangle. height
152
+ if !self . isSelected{
153
+ self . showList ( )
154
+ }
155
+ }
156
+
157
+ }
158
+ NotificationCenter . default. addObserver ( forName: NSNotification . Name. UIKeyboardWillHide, object: nil , queue: nil ) { ( notification) in
159
+ if self . isFirstResponder{
160
+ self . keyboardHeight = 0
161
+ }
162
+ }
163
+ }
130
164
}
165
+
166
+ deinit {
167
+ NotificationCenter . default. removeObserver ( self )
168
+ }
169
+
170
+
131
171
fileprivate func addGesture ( ) {
132
172
let gesture = UITapGestureRecognizer ( target: self , action: #selector( touchAction) )
133
173
if isSearchEnable{
@@ -157,9 +197,9 @@ open class DropDown : UITextField{
157
197
public func showList( ) {
158
198
if parentController == nil {
159
199
parentController = self . parentViewController
160
- backgroundView. frame = parentController? . view. frame ?? backgroundView. frame
161
- pointToParent = getConvertedPoint ( self , baseView: parentController? . view)
162
200
}
201
+ backgroundView. frame = parentController? . view. frame ?? backgroundView. frame
202
+ pointToParent = getConvertedPoint ( self , baseView: parentController? . view)
163
203
parentController? . view. insertSubview ( backgroundView, aboveSubview: self )
164
204
TableWillAppearCompletion ( )
165
205
if listHeight > rowHeight * CGFloat( dataArray. count) {
@@ -184,6 +224,11 @@ open class DropDown : UITextField{
184
224
parentController? . view. addSubview ( shadow)
185
225
parentController? . view. addSubview ( table)
186
226
self . isSelected = true
227
+ let height = ( self . parentController? . view. frame. height ?? 0 ) - ( self . pointToParent. y + self . frame. height + 5 )
228
+ var y = self . pointToParent. y+ self . frame. height+ 5
229
+ if height < ( keyboardHeight+ tableheightX) {
230
+ y = self . pointToParent. y - tableheightX
231
+ }
187
232
UIView . animate ( withDuration: 0.9 ,
188
233
delay: 0 ,
189
234
usingSpringWithDamping: 0.4 ,
@@ -192,16 +237,18 @@ open class DropDown : UITextField{
192
237
animations: { ( ) -> Void in
193
238
194
239
self . table. frame = CGRect ( x: self . pointToParent. x,
195
- y: self . pointToParent . y + self . frame . height + 5 ,
240
+ y: y ,
196
241
width: self . frame. width,
197
242
height: self . tableheightX)
198
243
self . table. alpha = 1
199
244
self . shadow. frame = self . table. frame
200
245
self . shadow. dropShadow ( )
201
246
self . arrow. position = . up
247
+
202
248
203
249
} ,
204
250
completion: { ( finish) -> Void in
251
+ self . layoutIfNeeded ( )
205
252
206
253
} )
207
254
@@ -244,21 +291,28 @@ open class DropDown : UITextField{
244
291
} else {
245
292
self . tableheightX = listHeight
246
293
}
294
+ let height = ( self . parentController? . view. frame. height ?? 0 ) - ( self . pointToParent. y + self . frame. height + 5 )
295
+ var y = self . pointToParent. y+ self . frame. height+ 5
296
+ if height < ( keyboardHeight+ tableheightX) {
297
+ y = self . pointToParent. y - tableheightX
298
+ }
247
299
UIView . animate ( withDuration: 0.2 ,
248
300
delay: 0.1 ,
249
301
usingSpringWithDamping: 0.9 ,
250
302
initialSpringVelocity: 0.1 ,
251
303
options: . curveEaseInOut,
252
304
animations: { ( ) -> Void in
253
305
self . table. frame = CGRect ( x: self . pointToParent. x,
254
- y: self . pointToParent . y + self . frame . height + 5 ,
306
+ y: y ,
255
307
width: self . frame. width,
256
308
height: self . tableheightX)
257
-
309
+ self . shadow. frame = self . table. frame
310
+ self . shadow. dropShadow ( )
258
311
259
312
} ,
260
313
completion: { ( didFinish) -> Void in
261
- self . shadow. layer. shadowPath = UIBezierPath ( rect: self . table. bounds) . cgPath
314
+ // self.shadow.layer.shadowPath = UIBezierPath(rect: self.table.bounds).cgPath
315
+ self . layoutIfNeeded ( )
262
316
263
317
} )
264
318
}
@@ -343,7 +397,7 @@ extension DropDown: UITableViewDataSource {
343
397
cell!. imageView!. image = UIImage ( named: imageArray [ indexPath. row] )
344
398
}
345
399
cell!. textLabel!. text = " \( dataArray [ indexPath. row] ) "
346
- cell!. accessoryType = indexPath. row == selectedIndex ? . checkmark : . none
400
+ cell!. accessoryType = ( indexPath. row == selectedIndex) && checkMarkEnabled ? . checkmark : . none
347
401
cell!. selectionStyle = . none
348
402
cell? . textLabel? . font = self . font
349
403
cell? . textLabel? . textAlignment = self . textAlignment
@@ -370,7 +424,7 @@ extension DropDown: UITableViewDelegate {
370
424
touchAction ( )
371
425
self . endEditing ( true )
372
426
}
373
- if let selected = optionArray. index ( where: { $0 == selectedText} ) {
427
+ if let selected = optionArray. firstIndex ( where: { $0 == selectedText} ) {
374
428
if let id = optionIds ? [ selected] {
375
429
didSelectCompletion ( selectedText, selected , id )
376
430
} else {
@@ -396,7 +450,13 @@ enum Position {
396
450
}
397
451
398
452
class Arrow : UIView {
399
-
453
+ let shapeLayer = CAShapeLayer ( )
454
+ var arrowColor : UIColor = . black {
455
+ didSet{
456
+ shapeLayer. fillColor = arrowColor. cgColor
457
+ }
458
+ }
459
+
400
460
var position : Position = . down {
401
461
didSet{
402
462
switch position {
@@ -419,7 +479,7 @@ class Arrow: UIView {
419
479
}
420
480
}
421
481
422
- init ( origin: CGPoint , size: CGFloat ) {
482
+ init ( origin: CGPoint , size: CGFloat ) {
423
483
super. init ( frame: CGRect ( x: origin. x, y: origin. y, width: size, height: size) )
424
484
}
425
485
@@ -445,8 +505,9 @@ class Arrow: UIView {
445
505
bezierPath. close ( )
446
506
447
507
// Mask to path
448
- let shapeLayer = CAShapeLayer ( )
449
508
shapeLayer. path = bezierPath. cgPath
509
+ // shapeLayer.fillColor = arrowColor.cgColor
510
+
450
511
if #available( iOS 12 . 0 , * ) {
451
512
self . layer. addSublayer ( shapeLayer)
452
513
} else {
@@ -468,10 +529,6 @@ extension UIView {
468
529
layer. rasterizationScale = scale ? UIScreen . main. scale : 1
469
530
}
470
531
471
-
472
- }
473
-
474
- extension UIView {
475
532
var parentViewController : UIViewController ? {
476
533
var parentResponder : UIResponder ? = self
477
534
while parentResponder != nil {
0 commit comments