gpt4 book ai didi

iOS。界面 TextView 。 UIKeyboardDidHideNotification 后文本跳转

转载 作者:可可西里 更新时间:2023-11-01 04:43:48 26 4
gpt4 key购买 nike

我有可编辑的 UITextView 并且键盘关闭模式是交互式的。此外,我的 Controller 正在监听两个通知:UIKeyboardWillShowNotificationUIKeyboardWillHideNotification

func keyboardWillShow(notification: NSNotification) {


if let userInfo = notification.userInfo {

var insets = self.textView.contentInset;
let rect = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() ?? CGRectZero
insets.bottom = (rect.size.height - (CGRectGetHeight(self.view.frame) - CGRectGetMaxY(self.textView.frame)))
self.textView.contentInset = insets
self.textView.scrollIndicatorInsets = insets
}
}


func keyboardWillHide(notification: NSNotification) {
self.textView.contentInset = UIEdgeInsetsZero
self.textView.scrollIndicatorInsets = UIEdgeInsetsZero
}

如果 UITextView 中的文本不包含任何空行,这个东西就很好用。如果是,contentOffset 跳到另一个随机位置。

我不确定这是 iOS 7+ 中的错误,还是我做错了什么。如果这不是错误,如何在没有跳跃行为的情况下流畅地进行?

感谢您的帮助。

最佳答案

我一直在与这个完全相同的问题作斗争,当我关闭键盘时,UITextView 的内容偏移量会跳回到 {0, 0}。有趣的是,我只在设备上得到了这种行为,而在模拟器中却没有。

我最初试图通过覆盖 UITextView 的 contentOffset 方法并让它忽略 {0, 0} 值来解决它,这是半有效的,直到内容得到太长了,在这种情况下,它只会跳到一个随机偏移量,并设置相同的值 3 次(因此它会将内容偏移量设置为 {0, 3605}, {0, 3605 }{0, 3605} 都快速连续)。

找了半天,发现其实很简单:

textview.layoutManager.allowsNonContiguousLayout = NO;

this blog post 中所述.希望有所帮助:)

关于iOS。界面 TextView 。 UIKeyboardDidHideNotification 后文本跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27622221/

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