I am trying to slide a textbox, which is located in the bottom of a tableview (actually, in its footer view). So I tried two methods:
Still, non of them worked, or partially worked. Currently, I am not able to test this on a real iPad (app is iPad only) and I am testing on Simulator, but I thought something like this should work:
Animating contentInsets
func keyboardWillShow(notification: NSNotification) { if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height { UIView.animate(withDuration: 0.2, animations: { self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0) }) } } func keyboardWillHide(notification: NSNotification) { UIView.animate(withDuration: 0.2, animations: { self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) }) }
Animating autolayout constraint
func keyboardWillShow(notification: NSNotification) { if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height { UIView.animate(withDuration: 0.2, animations: { self.bottomTableviewConstraint.constant = keyboardHeight self.qaTableview.layoutIfNeeded() }) } } func keyboardWillHide(notification: NSNotification) { UIView.animate(withDuration: 0.2, animations: { self.bottomTableviewConstraint.constant = 0 self.qaTableview.layoutIfNeeded() }) }
So in first example, I never noticed animation, nor tableview content moving. In the second example I made an outlet of a autolayout constraint (bottom vertical spacing) but I get some odd results. eg, tableview moves up, but not completely, and only for the first time.
If I put a breakpoint, keyboardHeight
is equal to 471
. I am I missing something obvious ?
This is what I tried and it worked perfectly fine as expected.
Can you check where you added your Notification center?
-- Animating contentInsets
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
UIView.animate(withDuration: 0.2, animations: {
self.tbl.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
})
}
}
func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
self.tbl.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
})
}
-- Animating autolayout constraint
Make sure you do not have both Bottom layout and height constraints set for UITableView in Storyboard.
NOTE
But there's an even more easy way to do this, just by using a UITableViewController
. It will take care of this feature by default. This is, of course, if you have no problem in using it.
EDIT
UITextView
will act a little different from UITextField
. Set delegate
for UITextView
. And do all table view alignments in textViewShouldBeginEditing
.
extension ViewController: UITextViewDelegate {
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
let pointInTable:CGPoint = textView.superview!.convert(textView.frame.origin, to: tbl)
tbl.contentOffset = CGPoint(x: tbl.contentOffset.x, y: pointInTable.y)
return true
}
}
I use IQKeyboardManager library, which handles this behavior: https://github.com/hackiftekhar/IQKeyboardManager
IQKeyboardManager allows you to prevent issues of the keyboard sliding up and cover UITextField/UITextView without needing you to enter any code and no additional setup required. To use IQKeyboardManager you simply need to add source files to your project.
It's really easy to implement. Import the library with pods, and just add, in your AppDelegate:
import IQKeyboardManager
And in didFinishLaunchingWithOptions:
IQKeyboardManager.sharedManager().enable = true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.