如何解决UITextView中的"auto-scroll"?
How to solve "auto-scroll" in UITextView?
我写了一个函数scrollToVisible()
来滚动UItextview
中的文本,因为文本的某些部分被键盘覆盖,或者光标不可见。但是UItextview
可以在光标不在整个视图中但不可见时自动滚动文本,它仍然可以被键盘自动覆盖scroll.The UItextview 的自动滚动可以打断我的scrollToVisible()
。
因此,我可以禁止 UItexview 自动滚动吗?或者用另一种方法解决 "keyboard cover" 问题?
我的 scrollToVisible() 函数
func scrollToVisible()
{
let cursortop = self.EditArea.convert(self.EditArea.caretRect(for: (self.EditArea.selectedTextRange?.start)!).origin, to: self.view)
var cursorbottom = cursortop
cursorbottom.y += self.EditArea.caretRect(for: (self.EditArea.selectedTextRange?.start)!).height
let bottom = UIScreen.main.bounds.size.height - self.EditArea.textContainerInset.bottom
var contentOffset = self.EditArea.contentOffset
if cursortop.y <= 85
{
contentOffset.y = contentOffset.y - 85 + cursortop.y
self.EditArea.setContentOffset(contentOffset, animated: true)
}
else if cursorbottom.y >= bottom
{
contentOffset.y = contentOffset.y - bottom + cursorbottom.y
self.EditArea.setContentOffset(contentOffset, animated: true)
}
}
PS:这个EditArea就是textview
这是我的解决方案,您无需担心在应用程序中处理任何 textfield/textview,只需在应用程序委托中编写一行代码
如果您使用的是 pods,则只需添加以下内容即可添加 "IQKeyboardManager" pods pods
连播 'IQKeyboardManagerSwift'
并在 app delegate 的 didFinishLaunchingWithOptions 中添加这一行
IQKeyboardManager.sharedManager().enable = true
我有类似的问题:当你打开键盘时,文本视图没有调整,光标隐藏在键盘后面(或者你说的 "covers" 光标)。因此,如果我按下 Enter 开始新的一行,它也不会明显地自动滚动(实际上它会自动滚动,但它在键盘后面)。我在这个网站上找到了一个非常适合我的解决方案:https://www.hackingwithswift.com/example-code/uikit/how-to-adjust-a-uiscrollview-to-fit-the-keyboard
使用swift 4从上述网站提取的解决方案:
在您的 viewDidLoad()
函数中订阅键盘出现和消失时的事件:
// For avoiding that the text cursor disappears behind the keyboard, adjust the text for it
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: .UIKeyboardWillHide, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: .UIKeyboardWillChangeFrame, object: nil)
使用此功能调整文本视图,将其添加到您的任意位置 class:
// Adjusts the textView, so that the text cursor does not disappear behind the keyboard
@objc func adjustForKeyboard(notification: Notification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide {
textView.contentInset = UIEdgeInsets.zero
} else {
textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
textView.scrollIndicatorInsets = textView.contentInset
let selectedRange = textView.selectedRange
textView.scrollRangeToVisible(selectedRange)
}
我写了一个函数scrollToVisible()
来滚动UItextview
中的文本,因为文本的某些部分被键盘覆盖,或者光标不可见。但是UItextview
可以在光标不在整个视图中但不可见时自动滚动文本,它仍然可以被键盘自动覆盖scroll.The UItextview 的自动滚动可以打断我的scrollToVisible()
。
因此,我可以禁止 UItexview 自动滚动吗?或者用另一种方法解决 "keyboard cover" 问题?
我的 scrollToVisible() 函数
func scrollToVisible()
{
let cursortop = self.EditArea.convert(self.EditArea.caretRect(for: (self.EditArea.selectedTextRange?.start)!).origin, to: self.view)
var cursorbottom = cursortop
cursorbottom.y += self.EditArea.caretRect(for: (self.EditArea.selectedTextRange?.start)!).height
let bottom = UIScreen.main.bounds.size.height - self.EditArea.textContainerInset.bottom
var contentOffset = self.EditArea.contentOffset
if cursortop.y <= 85
{
contentOffset.y = contentOffset.y - 85 + cursortop.y
self.EditArea.setContentOffset(contentOffset, animated: true)
}
else if cursorbottom.y >= bottom
{
contentOffset.y = contentOffset.y - bottom + cursorbottom.y
self.EditArea.setContentOffset(contentOffset, animated: true)
}
}
PS:这个EditArea就是textview
这是我的解决方案,您无需担心在应用程序中处理任何 textfield/textview,只需在应用程序委托中编写一行代码
如果您使用的是 pods,则只需添加以下内容即可添加 "IQKeyboardManager" pods pods
连播 'IQKeyboardManagerSwift'
并在 app delegate 的 didFinishLaunchingWithOptions 中添加这一行
IQKeyboardManager.sharedManager().enable = true
我有类似的问题:当你打开键盘时,文本视图没有调整,光标隐藏在键盘后面(或者你说的 "covers" 光标)。因此,如果我按下 Enter 开始新的一行,它也不会明显地自动滚动(实际上它会自动滚动,但它在键盘后面)。我在这个网站上找到了一个非常适合我的解决方案:https://www.hackingwithswift.com/example-code/uikit/how-to-adjust-a-uiscrollview-to-fit-the-keyboard
使用swift 4从上述网站提取的解决方案:
在您的 viewDidLoad()
函数中订阅键盘出现和消失时的事件:
// For avoiding that the text cursor disappears behind the keyboard, adjust the text for it
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: .UIKeyboardWillHide, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: .UIKeyboardWillChangeFrame, object: nil)
使用此功能调整文本视图,将其添加到您的任意位置 class:
// Adjusts the textView, so that the text cursor does not disappear behind the keyboard
@objc func adjustForKeyboard(notification: Notification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide {
textView.contentInset = UIEdgeInsets.zero
} else {
textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
textView.scrollIndicatorInsets = textView.contentInset
let selectedRange = textView.selectedRange
textView.scrollRangeToVisible(selectedRange)
}