I am having a tableview with textfield present in the tableview cell's.when i try to enter text particularly in Textfield present at the bottom of tableview,then textfields are hidden with keyboard. I am trying to solve this problem by using code present below.
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyBoardWillChange(notification : Notification)
{
print("Keyboard will show :\(notification.name.rawValue)")
let cell = deliveryTableView.cellForRow(at: IndexPath.init(row: 8, section: 0))
view.frame.origin.y = -175
}/*
@objc func KeyBoardWillHide(notification : Notification)
{
UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions.allowUserInteraction, animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
}, completion: nil)
}*/
deinit {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
But the problem is that tableview shift upward even when clicking on very first textfield,which does not looks good .But i want tableview to shift upward only in case of textfield's present at the bottom.
Use IQKeyboardManager with just single line of code in appdelegate.It will take care of keyboard.
step 1) Add pod 'IQKeyboardManager' in pod-file and run pod install
step 2) Enable IQKeyboardManager by writing this piece of code in didFinishLaunchingWithOptions method of appdelegate
IQKeyboardManager.shared.enable = true