1

以下のような機能のチャットを作成しています。

ここに画像の説明を入力してください

ユーザーがメッセージバブルをクリックすると、UITextFieldとUITableViewの両方を表示する必要があります(もちろん、テキストボックスの上にテーブルビューがあります)。同様に、メッセージを送信または撤回すると、メッセージは元の状態に戻るはずです。

ここに掲載されている解決策を試しました

2つの通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object: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];
}

しかし、2つの問題があります。

  1. キーボードの上にテーブルビューを上げる方法がわかりません
  2. キーボードが下がると、入力ボックスは完全に消えます。
4

1 に答える 1

2

これは役立つかもしれません....それを信用することはできませんが、私の人生では、それがどこから来たのか思い出せません... ある時点でプロジェクトを助けてくれたので、これが私がそれを使用した方法です.

これは、BOOL 値 YES または NO に基づいてキーボードが呼び出された/閉じられたときに、ビューを上下に移動します。このようにして、他の側面をもう少し制御することもできます。

- (void) animateTextField: (UITextField*) textField up: (BOOL)up
{
const int movementDistance = 100;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

そして、それを実装するには:

-(IBAction)goingup:(id)sender
{
   //Bounces the text field up for editing
   [self animateTextField: yourtextfield up: YES];} 
}


-(IBAction)backdown:(id)sender
{
   //Bounces it back when keyboard is dismissed 
   [self animateTextField: yourtextfield up: NO];   
}

アクションをテキスト フィールド Editing did begin および Editing did end アウトレットに接続すると、準備完了です。

それが役立つことを願っています。

于 2013-01-12T03:07:27.060 に答える