gpt4 book ai didi

ios - 自动滚动到放置在 UITableViewCell 内的 textView 光标在 iOS11 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:41 27 4
gpt4 key购买 nike

我在编辑时根据 cell 内的 UITextView 光标位置具有 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
}

关于ios - 自动滚动到放置在 UITableViewCell 内的 textView 光标在 iOS11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47708491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com