4

私のアプリでは、テキストフィールドをクリックすると、キーボードで非表示になります。助けてください-テキストフィールドをクリックしたときにビューを上に移動するにはどうすればよいですか。私はこのコードを使用していますtextFieldDidBeginEditing:

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

しかし、それは機能しません。

4

2 に答える 2

13

textFieldDidBeginEditing:ユーザーが画面キーボードが表示されない物理キーボードを使用して入力している場合でも、このメソッドが呼び出されるため、キーボードの調整を信頼しないでください。

UIKeyboardWillShowNotification代わりに、キーボードが実際に表示されるときにのみトリガーされるを聞いてください。3つのステップのプロセスを実行する必要があります。

  1. 通知userInfo辞書からキーボードの実際のサイズを決定します。サイズは、横向き/縦向き、およびさまざまなデバイスとは異なります。
  2. contentInset決定したサイズを使用して更新します。あなたはそれをアニメーション化することができます、通知はあなたにキーボードアニメーションの持続時間を教えさえします。
  3. テキストフィールドをスクロールして表示します。これは忘れがちです。

詳細とサンプルコードはここから入手できます

于 2011-05-11T14:20:38.633 に答える
1

次のことができますが、最初にUITextFieldデリゲートを自分自身に設定していることを確認してください。

#define kOFFSET_FOR_KEYBOARD 350;

頂点で。これは、ビューをシフトする距離です

//method to move the view up/down whenever the keyboard is shown/dismissed

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.

        if (rect.origin.y == 0 ) {
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            //rect.size.height += kOFFSET_FOR_KEYBOARD;
        }

    }
    else
    {
        if (stayup == NO) {
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
    }
    self.view.frame = rect; 
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMovedUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMovedUp:YES];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    stayup = YES;
    [self setViewMovedUp:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    stayup = NO;
    [self setViewMovedUp:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
于 2011-05-11T14:11:52.623 に答える