I found a solution of this here:
How to scroll UITextView above Keyboard in a UITableView in swift
But I want it to be right above the keyboard +10-20px.
I tried this to add an observer in viewDidLoad() to find out keyboard height:
var keyboardSize: CGRect?
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
println(keyboardSize)
}
}
Then in textViewShouldBeginEditing I get a point from textView tableViewCell to tableView and trying to subtract keyboard.height from contentOffset.y value.
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
var pointInTable:CGPoint = textView.superview!.convertPoint(textView.superview!.frame.origin, toView: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if keyboardSize != nil {
contentOffset.y -= keyboardSize!.height
}
tableView.contentOffset = contentOffset
return true
}
When i run the application and click in textView for the first time it doesnt work but for the second time it works like this.
Everything is fine but I want it to work for the first time and one more thing: if i click on textView in cell at the bottom of tableView and then click in textView in the first cell it will work like this. But i want it to be at the top of the screen if keyboard doesn't hide it.