I am trying to use below code to move TextView up when keyboard hide it.
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifiactions()
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 40
tableView.rowHeight = UITableViewAutomaticDimension
}
var textsView = UITextView()
func registerForKeyboardNotifiactions(){
let ncUp = NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
var ncDown = NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(aNotification: NSNotification) {
let info = aNotification.userInfo!
let kbSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue().size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
tableView.contentInset = contentInsets
tableView.scrollIndicatorInsets = contentInsets
var aRect = self.view.frame
aRect.size.height -= kbSize.height
if !CGRectContainsPoint(aRect, textsField.frame.origin) {
self.tableView.scrollRectToVisible(textsField.frame , animated: true)
}
else if !CGRectContainsPoint(aRect, textsView.frame.origin) {
self.tableView.scrollRectToVisible(textsView.frame , animated: true)
}
}
func keyboardWillBeHidden(aNotifiacation: NSNotification){
let contentInsets = UIEdgeInsetsZero
tableView.contentInset = contentInsets
tableView.scrollIndicatorInsets = contentInsets
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cells = tableView.dequeueReusableCellWithIdentifier("CommentCell", forIndexPath: indexPath) as! MakeCommentCell
self.textsView = cells.textView
cells.textView.delegate = self
textsField = cells.textField return cells
}
It work fine with TextField but in case of TextView its not working. I tried lots of things as to change bottom constraints and other suggestion that I could find in Stack OverFlow and other site but none seems to work for me (I might not have understood it clearly).
How could I make it work?
If there is better way I would love to know it.
Thank you
I think there is some thing wrong with the part of your code that checks whether the textField/textViews frame is covered by the keyboard.
Before you compare CGGeometry structs you should make sure they are from the same coordinate space. The frame property of the textView is on the coordinate space of it's superview, w/c I understand is the contentView of a table cell. You should probably not compare it directly with view.frame w/c is on the coordinate space of view.superview.
UIView and UICoordinateSpace has methods to convert these structs.
Anyway, here's how I would do the check:
let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()//the keyboard end frame on the coordinate space of the app's window
let textViewFrame = textView.convertRect(textView.bounds, toView: nil)//the textView frame on the coordinate space of the app's window
if CGRectIntersectsRect(keyboardFrame, textViewFrame) {
let textViewFrame = textView.convertRect(textView.bounds, toView:self.tableView) //textView frame on the coord space of tableView
self.tableView.scrollRectToVisible(textViewFrame , animated: 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.