フィールドが覆われないようにキーボードが表示されているときに画面をスクロールするコードを実装しました。キーボードが表示されているときに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関数内であるということです。
私はこれを把握できないので、助力をいただければ幸いです。
ありがとうございます!