12

私は現在、キーボードの上にUITextFieldを持っています。あなたがそれをタップすると、それはキーボードの上に固執し、スムーズに上に移動する必要があります。私はキーボードの正確な時間とアニメーションの種類を知らないので、それは本当にうんざりです。私が持っているものここにあります:becomeFirstResponderまたはresignFirstResponderが発生した場合、キーボードの上にオブジェクトを保持しますか?

[theTextView resignFirstResponder]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
// Frame changes go here (move down 216px) 
[UIView commitAnimations]; 

[theTextView becomeFirstResponder]; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
// Frame changes go here (move up 216px) 
[UIView commitAnimations]; 

誰もが前にこのような何かを行っている場合、私はあなたがアニメーションを滑らかにするために使用される設定を知っているように、バーの上に「スタック」であることを見せることでしょうキーボード。

答えて

44

UIKit投稿UIKeyboardWillShowNotificationキーボードを表示するときはUIKeyboardWillHideNotification、キーボードを表示しないときはUIKeyboardWillHideNotificationです。これらの通知には、UITextFieldを適切にアニメーション化するために必要なものがすべて含まれています。

あなたのUITextFieldは、myTextFieldという名前のプロパティにあるとします。

まず、通知のためにどこかに登録する必要があります。登録する場所は、移動するオブジェクトが何であるかによって異なります。myTextField私のプロジェクトでは、フィールドのスーパーには責任がある、と私はペン先からの私のUIをロードするので、私はスーパーのawakeFromNibでそれを行う:

- (void)awakeFromNib 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil]; 
} 

あなたの周りのフィールドを移動するためにUIViewControllerを使用している場合、あなたはおそらくよviewWillAppear:animated:でそれをやりたいもちろん

- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

トリッキーなビットがkeyboardWillHideOrShow:方法である:

あなたのdeallocまたはviewWillDisappear:animated:に登録解除する必要があります。

- (void)keyboardWillHideOrShow:(NSNotification *)note 
{ 
    NSDictionary *userInfo = note.userInfo; 
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
keyboardFrame

グローバル座標系である:最初のIは、通知からアニメーションパラメータを抽出します。私はmyTextField.frameと同じ座標系にフレームを変換する必要があり、かつmyTextField.framemyTextField.superviewの座標系である:

CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil]; 

次に、私はmyTextFieldに移動するフレームを計算します。キーボードを使用している、私は同じアニメーションパラメータを使用して、その新しいフレームにmyTextFieldをアニメート最後

CGRect newTextFieldFrame = self.myTextField.frame; 
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height; 

:新しいフレームの下端には、キーボードのフレームの上端に等しくなければならない

ここで
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ 
     self.myTextField.frame = newTextFieldFrame; 
    } completion:nil]; 
} 

それはすべて一緒に入れている:

- (void)keyboardWillHideOrShow:(NSNotification *)note 
{ 
    NSDictionary *userInfo = note.userInfo; 
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil]; 

    CGRect newTextFieldFrame = self.myTextField.frame; 
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height; 

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ 
     self.myTextField.frame = newTextFieldFrame; 
    } completion:nil]; 
} 
+1

ありがとうございます。ありがとうございました。ありがとうございました。ありがとうございました。少し修正して、私はそれを働かせて、それはスーパースリックを見ています。 – iosfreak

+0

@rob mayoff問題は私が 'UITabBar'をしたときです。キーボードが 'UITextField'を隠すと、' UITabBar'の下にスライドします。どうして? –

+0

タブバーがある場合、キーボードが隠れているときは、キーボードのフレームの代わりにタブバーのフレームに基づいて 'newTextFieldFrame'を計算する必要があります。 –

0

setContentOffset:animation:メソッドを使用してUITextFieldをキーボードにドッキングさせるには、オフセット計算を行い、スクロールビューにオフセット変更を適用する必要があります(UITextFieldがUIScrollViewに配置されていると仮定します)。

+0

ScrollViewにはありません。 scrollViewは存在しません、それはUITableViewです。 – iosfreak

+2

@ phpnerd211:UITableViewはUIScrollViewのサブクラスです。 – Andrew

5

設定し、あなたのテキストフィールド(またはテキストフィールドを保持するビュー)フィールドのinputAccessoryViewとしてそのあなたは編集中です。それは自動的にキーボードの上部に取り付けられ、適切にアニメートされます。

関連する問題