我在编辑时具有基于UITextViewcell光标位置的tableView的自动滚动功能。

它可以在以前的iOS版本中使用。从iOS11开始,它已损坏。

我已经根据键盘高度设置了tableView contentInset。对于自动滚动,我在textViewDidChange中使用以下代码

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)

        }

最佳答案

Askh1t是正确的,这是我的实现:

    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
    }

以及应该为您工作的完整模块化实现:
//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
}

10-08 07:27