gpt4 book ai didi

ios - 显示键盘时 UITextView 不会滚动

转载 作者:行者123 更新时间:2023-11-30 13:24:26 25 4
gpt4 key购买 nike

我已经实现了一些代码来在显示键盘时滚动屏幕,以防止字段被覆盖。

这对于文本字段工作正常,但对于 UITextView 字段不起作用。

这是我实现的代码:

  func textFieldDidBeginEditing(textField: UITextField)
{
//print("textfieldDidBeginEditing")
activeField = textField
}

func textFieldDidEndEditing(textField: UITextField)
{
//print("textfieldDidEndEditing")
activeField = nil
}


func textViewDidBeginEditing(textView: UITextView) {
print("textViewDidBeginEditing")
//print(textView)
activeTextView = textView
print("1activeTextView:\(activeTextView)")


}

func textViewDidEndEditing(textView: UITextView) {
print("textViewDidEndEditing")
//print(textView)
//print("2activeTextView:\(activeTextView)")
activeTextView = nil
}



func registerForKeyboardNotifications()
{
//Adding notifies on keyboard appearing
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
}


func deregisterFromKeyboardNotifications()
{
//Removing notifies on keyboard appearing
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWasShown(notification: NSNotification)
{
print("yes")
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.scrollEnabled = true
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size


let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets

var aRect : CGRect = self.view.frame
aRect.size.height = keyboardSize!.height



if (activeField != nil) {
print("KWSactivfield:\(activeField)")
if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
{
print("!CGRectContainsPoint")
self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
}
}

if (activeTextView != nil) {
print("KWSactiveTextView:\(activeTextView)")
if (!CGRectContainsPoint(aRect, activeTextView!.frame.origin))
{
print("true4")
self.scrollView.scrollRectToVisible(activeTextView!.frame, animated: true)
}
}



}


func keyboardWillBeHidden(notification: NSNotification)
{
//Once keyboard disappears, restore original positions
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -2*keyboardSize!.height, 0.0)
print("insets")
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.scrollEnabled = false

}

我注意到,一旦进入keyboardWasShown函数,activeTextView总是“nil”。

任何帮助将不胜感激,因为我似乎无法弄清楚这一点。

谢谢!

最佳答案

我知道这是一篇旧帖子,但我遇到了同样的问题,我用一个小技巧解决了它。我在这里发布我的解决方案,可能对其他人有帮助。

诀窍是为所有 TextView 的标签属性分配一个值,然后在我的keyboardWasShown方法中读取它,并且根据这个值,我知道选择了哪个 TextView 。一些代码将帮助您理解我做了什么:

我声明了我的textviews和一个int变量来存储所选textview的标签值:

var myTextViewField: UITextView! = nil
var anotherTextViewField: UITextView! = nil
var activeTextViewTag: Int = 0

在 viewDidLoad 中,我设置了 TextView 的标记属性:

myTextViewField.tag = 1
anotherTextViewField.tag = 2

现在是时候读取标签值了,我是在 textViewDidBeginEditing 方法中完成的

func textViewDidBeginEditing(_ textView: UITextView) {
activeTextViewTag = textView.tag
}

最后,在keyboardWasShown方法中读取activeTextViewTag值并移动 ScrollView

func keyboardWasShown(notification: NSNotification){
print("keyboardWasShown")
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
var aRect: CGRect = self.view.frame
aRect.size.height = aRect.size.height - keyboardSize.height
var activeField: UITextView! = nil
if(activeTextViewTag == 1){
activeField = myTextViewField
} else {
activeField = anotherTextViewField
}

if(!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}

} else {
// no UIKeyboardFrameBeginUserInfoKey entry in userInfo
print("no UIKeyboardFrameBeginUserInfoKey entry in userInfo")
}
} else {
// no userInfo dictionary in notification
print("no userInfo dictionary in notification")
}
}

我直接在keyboardWasShown中检查了事件字段,但它可以在单独的方法中完成。就这样!这个解决方案非常适合我,我希望这可以帮助其他人!

关于ios - 显示键盘时 UITextView 不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394636/

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