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.frame
はmyTextField.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];
}
ありがとうございます。ありがとうございました。ありがとうございました。ありがとうございました。少し修正して、私はそれを働かせて、それはスーパースリックを見ています。 – iosfreak
@rob mayoff問題は私が 'UITabBar'をしたときです。キーボードが 'UITextField'を隠すと、' UITabBar'の下にスライドします。どうして? –
タブバーがある場合、キーボードが隠れているときは、キーボードのフレームの代わりにタブバーのフレームに基づいて 'newTextFieldFrame'を計算する必要があります。 –