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

这对于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”。

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

谢谢!

最佳答案

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

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

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

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


在viewDidLoad与所有其他属性,我已经设置了我的textviews的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中检查了活动字段,但是可以用单独的方法来完成。
就这样!该解决方案非常适合我,希望对您有所帮助!

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

10-09 13:02