显示键盘时 UITextView 不会滚动

Posted

技术标签:

【中文标题】显示键盘时 UITextView 不会滚动【英文标题】:UITextView won't scroll when keyboard is displayed 【发布时间】:2016-05-23 15:14:06 【问题描述】:

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

这适用于 Text 字段,但不适用于 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”。

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

谢谢!

【问题讨论】:

【参考方案1】:

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

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

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

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

在 viewDidLoad 中,我设置了我的 textviews 的所有其他属性:

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 中检查了活动字段,但可以通过单独的方法完成。 就这样!这个解决方案非常适合我,我希望这可以帮助其他人!

【讨论】:

以上是关于显示键盘时 UITextView 不会滚动的主要内容,如果未能解决你的问题,请参考以下文章

当键盘出现在swift中时滚动UITextView

键盘消失时,UIScrollView 不会向下滚动到其原始位置

当键盘可见时如何使 UITextView 一直滚动到底部

iPhone - 出现键盘时如何滚动 UITextField 并调整 UITextView 的大小

UITextView 避免键盘滚动

如何将 UITableViewCell 中的 UITextView 滚动到键盘顶部?