Autoscroll for textView cursor that is inside UITableViewCell not working in iOS11

I have a tableView auto-scroll function based on the location of the cursor UITextViewinside cellwhile editing.

He worked in previous versions of iOS. Starting with iOS11, it is broken.

I set tableView contentInsetbased on the height of the keyboard. To autoscroll am using the following code intextViewDidChange

if let confirmedTextViewCursorPosition = textView.selectedTextRange?.end {

            let caretPosition = textView.caretRect(for: confirmedTextViewCursorPosition)
            var textViewActualPosition = tableView.convert(caretPosition, from: textView.superview?.superview)
            textViewActualPosition.origin.y += 22.0 
            tableView.scrollRectToVisible(textViewActualPosition, animated: false)

        }
+4
source share
1 answer

Askh1t is correct, here is my implementation:

    var info = notification.userInfo!
    var keyboardSize:CGRect = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    if keyboardSize.size.height <= 0 { // to fix bug on iOS 11
        keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    }

as well as a complete modular implementation that should work for you:

//MARK: - Properties
var activeTextView: UITextView?

//MARK: - Scroll View Notifications
// add in viewDidLoad
func registerForKeyboardNotifications(){
    //Adding notifies on keyboard appearing
    NotificationCenter.default.addObserver(forName: Notification.Name.UIKeyboardWillShow, object: nil, queue: nil, using: keyboardWasShown)
    NotificationCenter.default.addObserver(forName: Notification.Name.UIKeyboardWillHide, object: nil, queue: nil, using: keyboardWillBeHidden)
}

//add in viewWillDisappear
func deregisterFromKeyboardNotifications(){
    //Removing notifies on keyboard appearing
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWasShown(notification: Notification) -> Void {
    //Need to calculate keyboard exact size due to Apple suggestions
    self.tableView.isScrollEnabled = true
    var info = notification.userInfo!
    var keyboardSize:CGRect = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    if keyboardSize.size.height <= 0 { // to fix bug on iOS 11
        keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    }
    self.tableView.contentInset.bottom = keyboardSize.height //add this much
    self.tableView.scrollIndicatorInsets.bottom = keyboardSize.height //scroll too it.

    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    if let activeField = self.activeTextView {
        if (!aRect.contains(activeField.frame.origin)){
            self.tableView.scrollRectToVisible(activeField.frame, animated: true)
        }
    }
}

func keyboardWillBeHidden(notification: Notification){
    self.tableView.contentInset.bottom = 0
    self.tableView.isScrollEnabled = true
    self.tableView.alwaysBounceVertical = true
}

func textViewDidBeginEditing(_ textView: UITextView){
    activeTextView = textView

}


func textViewDidEndEditing(_ textView: UITextView){
    tableView.isScrollEnabled = true
    activeTextView = nil
}
+1
source

Source: https://habr.com/ru/post/1690418/


All Articles