8000 KeyBoard Handling · jriosdev/iOSDropDown@e1ff1e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1ff1e3

Browse files
committed
KeyBoard Handling
1 parent 3f454fe commit e1ff1e3

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

iOSDropDown.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'iOSDropDown'
11-
s.version = '0.2.9'
11+
s.version = '0.3.1'
1212
s.summary = ' iOSDropDown is an Awesome DropDown Library with Search and other customization options'
1313
s.swift_version = '4.0'
1414
s.description = <<-DESC

iOSDropDown/Classes/iOSDropDown.swift

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ open class DropDown : UITextField{
5858
fileprivate var parentController:UIViewController?
5959
fileprivate var pointToParent = CGPoint(x: 0, y: 0)
6060
fileprivate var backgroundView = UIView()
61-
61+
fileprivate var keyboardHeight:CGFloat = 0
6262

6363
public var optionArray = [String]() {
6464
didSet{
@@ -137,7 +137,30 @@ open class DropDown : UITextField{
137137
self.backgroundView = UIView(frame: .zero)
138138
self.backgroundView.backgroundColor = .clear
139139
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+
}
140157
}
158+
159+
deinit {
160+
NotificationCenter.default.removeObserver(self)
161+
}
162+
163+
141164
fileprivate func addGesture (){
142165
let gesture = UITapGestureRecognizer(target: self, action: #selector(touchAction))
143166
if isSearchEnable{
@@ -194,6 +217,11 @@ open class DropDown : UITextField{
194217
parentController?.view.addSubview(shadow)
195218
parentController?.view.addSubview(table)
196219
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+
}
197225
UIView.animate(withDuration: 0.9,
198226
delay: 0,
199227
usingSpringWithDamping: 0.4,
@@ -202,16 +230,18 @@ open class DropDown : UITextField{
202230
animations: { () -> Void in
203231

204232
self.table.frame = CGRect(x: self.pointToParent.x,
205-
y: self.pointToParent.y+self.frame.height+5,
233+
y: y,
206234
width: self.frame.width,
207235
height: self.tableheightX)
208236
self.table.alpha = 1
209237
self.shadow.frame = self.table.frame
210238
self.shadow.dropShadow()
211239
self.arrow.position = .up
240+
212241

213242
},
214243
completion: { (finish) -> Void in
244+
self.layoutIfNeeded()
215245

216246
})
217247

@@ -254,21 +284,28 @@ open class DropDown : UITextField{
254284
}else{
255285
self.tableheightX = listHeight
256286
}
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+
}
257292
UIView.animate(withDuration: 0.2,
258293
delay: 0.1,
259294
usingSpringWithDamping: 0.9,
260295
initialSpringVelocity: 0.1,
261296
options: .curveEaseInOut,
262297
animations: { () -> Void in
263298
self.table.frame = CGRect(x: self.pointToParent.x,
264-
y: self.pointToParent.y+self.frame.height+5,
299+
y: y,
265300
width: self.frame.width,
266301
height: self.tableheightX)
267-
302+
self.shadow.frame = self.table.frame
303+
self.shadow.dropShadow()
268304

269305
},
270306
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()
272309

273310
})
274311
}
@@ -306,7 6359 +343,7 @@ extension DropDown : UITextFieldDelegate {
306343
textField.text = ""
307344
//self.selectedIndex = nil
308345
self.dataArray = self.optionArray
309-
touchAction()
346+
// touchAction()
310347
}
311348
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
312349
return isSearchEnable

0 commit comments

Comments
 (0)
0