@@ -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{
@@ -137,7 +137,30 @@ open class DropDown : UITextField{
137
137
self . backgroundView = UIView ( frame: . zero)
138
138
self . backgroundView. backgroundColor = . clear
139
139
addGesture ( )
140
+ if isSearchEnable{
141
+ NotificationCenter . default. addObserver ( forName: NSNotification . Name. UIKeyboardWillShow, object: nil , queue: nil ) { ( notification) in
142
+ if self . isFirstResponder{
143
+ let userInfo : NSDictionary = notification. userInfo! as NSDictionary
144
+ let keyboardFrame : NSValue = userInfo. value ( forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
145
+ let keyboardRectangle = keyboardFrame. cgRectValue
146
+ self . keyboardHeight = keyboardRectangle. height
147
+ self . showList ( )
148
+ }
149
+
150
+ }
151
+ NotificationCenter . default. addObserver ( forName: NSNotification . Name. UIKeyboardWillHide, object: nil , queue: nil ) { ( notification) in
152
+ if self . isFirstResponder{
153
+ self . keyboardHeight = 0
154
+ }
155
+ }
156
+ }
140
157
}
158
+
159
+ deinit {
160
+ NotificationCenter . default. removeObserver ( self )
161
+ }
162
+
163
+
141
164
fileprivate func addGesture ( ) {
142
165
let gesture = UITapGestureRecognizer ( target: self , action: #selector( touchAction) )
143
166
if isSearchEnable{
@@ -194,6 +217,11 @@ open class DropDown : UITextField{
194
217
parentController? . view. addSubview ( shadow)
195
218
parentController? . view. addSubview ( table)
196
219
self . isSelected = true
220
+ let height = ( self . parentController? . view. frame. height ?? 0 ) - ( self . pointToParent. y + self . frame. height + 5 )
221
+ var y = self . pointToParent. y+ self . frame. height+ 5
222
+ if height < ( keyboardHeight+ tableheightX) {
223
+ y = self . pointToParent. y - tableheightX
224
+ }
197
225
UIView . animate ( withDuration: 0.9 ,
198
226
delay: 0 ,
199
227
usingSpringWithDamping: 0.4 ,
@@ -202,16 +230,18 @@ open class DropDown : UITextField{
202
230
animations: { ( ) -> Void in
203
231
204
232
self . table. frame = CGRect ( x: self . pointToParent. x,
205
- y: self . pointToParent . y + self . frame . height + 5 ,
233
+ y: y ,
206
234
width: self . frame. width,
207
235
height: self . tableheightX)
208
236
self . table. alpha = 1
209
237
self . shadow. frame = self . table. frame
210
238
self . shadow. dropShadow ( )
211
239
self . arrow. position = . up
240
+
212
241
213
242
} ,
214
243
completion: { ( finish) -> Void in
244
+ self . layoutIfNeeded ( )
215
245
216
246
} )
217
247
@@ -254,21 +284,28 @@ open class DropDown : UITextField{
254
284
} else {
255
285
self . tableheightX = listHeight
256
286
}
287
+ let height = ( self . parentController? . view. frame. height ?? 0 ) - ( self . pointToParent. y + self . frame. height + 5 )
288
+ var y = self . pointToParent. y+ self . frame. height+ 5
289
+ if height < ( keyboardHeight+ tableheightX) {
290
+ y = self . pointToParent. y - tableheightX
291
+ }
257
292
UIView . animate ( withDuration: 0.2 ,
258
293
delay: 0.1 ,
259
294
usingSpringWithDamping: 0.9 ,
260
295
initialSpringVelocity: 0.1 ,
261
296
options: . curveEaseInOut,
262
297
animations: { ( ) -> Void in
263
298
self . table. frame = CGRect ( x: self . pointToParent. x,
264
- y: self . pointToParent . y + self . frame . height + 5 ,
299
+ y: y ,
265
300
width: self . frame. width,
266
301
height: self . tableheightX)
267
-
302
+ self . shadow. frame = self . table. frame
303
+ self . shadow. dropShadow ( )
268
304
269
305
} ,
270
306
completion: { ( didFinish) -> Void in
271
- self . shadow. layer. shadowPath = UIBezierPath ( rect: self . table. bounds) . cgPath
307
+ // self.shadow.layer.shadowPath = UIBezierPath(rect: self.table.bounds).cgPath
308
+ self . layoutIfNeeded ( )
272
309
273
310
} )
274
311
}
@@ -306,7
6359
+343,7 @@ extension DropDown : UITextFieldDelegate {
306
343
textField. text = " "
307
344
//self.selectedIndex = nil
308
345
self . dataArray = self . optionArray
309
- touchAction ( )
346
+ // touchAction()
310
347
}
311
348
public func textFieldShouldBeginEditing( _ textField: UITextField ) -> Bool {
312
349
return isSearchEnable
0 commit comments