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