0

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.

Community
  • 1
  • 1
mkz
  • 2,302
  • 2
  • 30
  • 43
  • 1
    just add some code, before you expect an answer. – Vizllx May 04 '15 at 13:15
  • 1
    You need to show what you've tried, and explain in detail how it fails to meet your needs. – Duncan C May 04 '15 at 13:29
  • Please don't put the platform and language in the title. We have tags for that matter. – Desdenova May 05 '15 at 07:26
  • You should have a look at [this](https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7). Apples way to handle your problem. Not exactly what you are looking for. But follows same principle. – Vivek Molkar May 05 '15 at 07:26
  • Removed broken image, removed extraneous commentary. – bgilham May 07 '15 at 12:12

0 Answers0