2016-05-23 10 views
0

フィールドが覆われないようにキーボードが表示されているときに画面をスクロールするコードを実装しました。キーボードが表示されているときにUITextViewがスクロールしない

これはテキストフィールドでは問題なく動作しますが、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は常に「ゼロ」一度keyboardWasShown関数内であるということです。

私はこれを把握できないので、助力をいただければ幸いです。

ありがとうございます!

答えて

0

これは古い投稿だと知っていますが、私は同じ問題を抱えていて、ちょっとした工夫で解決しました。私は他の誰かのために役立つかもしれない私の解決策をここに掲載します。

私のすべてのtextviewsのタグプロパティに値を割り当て、次にkeyboardWasShownメソッドで値を割り当て、この値に応じてどのtextviewが選択されたかを知っています。いくつかのコードは、あなたは私が何をしたかを理解するのに役立ちます:

私が選択したのTextViewのタグ値格納するために私のtextviewsとint型の変数を宣言:私が設定した他のすべてのプロパティでのviewDidLoadで

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

を私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直接アクティブフィールドをチェックし、それを分離する方法で行うことができます。 それだけです!このソリューションは完全に私のために働いた、私はこれが他の誰かを助けることを願って!

関連する問題