当键盘显示时,UITextView不会滚动

时间:2016-05-23 15:14:06

标签: ios swift

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

这适用于文本字段,但它不适用于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

}

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

任何帮助都会非常感激,因为我似乎无法解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

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

诀窍是为我的所有textview的tag属性赋值,然后在我的keyboardWasShown方法中读取它,并且根据这个值,我知道选择了哪个textview。有些代码可以帮助您理解我的所作所为:

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

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

在viewDidLoad中包含所有其他属性我已设置textview的tag属性:

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中检查了活动字段,但它可以在一个单独的方法中完成。 这就是全部!这个解决方案对我来说非常有用,我希望这可以帮助别人!