54

私は現在、入力用の複数のUITextFieldを持つ単一のビューを持つiPhoneアプリケーションに取り組んでいます。キーボードが表示されると、下部のテキストフィールドにオーバーレイされます。そこで、対応するtextFieldDidBeginEditing:メソッドを追加して、ビューを上に移動しました。これはうまく機能します。

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ( ( textField != inputAmount ) && ( textField != inputAge ) ) {
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= kOFFSET_FOR_KEYBOARD;
        frame.size.height += kOFFSET_FOR_KEYBOARD;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];      
    }
}

このメソッドは、メッセージのソースがキーボードの表示時に表示されるテキストフィールドの1つであるかどうかをチェックし、そうでない場合はビューを上に移動します。

textFieldDidEndEnditing:また、ビューを再び下に移動する(そして、変更された入力に従っていくつかのモデルオブジェクトを更新する)メソッドを追加しました。

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ( ( textField != inputMenge ) && ( textField != inputAlter ) ) {
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += kOFFSET_FOR_KEYBOARD;
        frame.size.height -= kOFFSET_FOR_KEYBOARD;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];      
    }
    // Additional Code
}

ただし、このソリューションには単純な欠陥があります。「非表示」テキストフィールドの1つを編集し終えて別のテキストフィールドに触れると、キーボードが消え、ビューが下に移動し、ビューが再び上に移動して、キーボードが再び表示されます。

(「非表示」テキストフィールドの)2つの編集の間にキーボードが消えて再表示されないようにする可能性はありますか?選択したテキストフィールドがキーボードによって非表示になるものから非表示にならないものに変更されたときにのみビューが移動するようにします)?

4

7 に答える 7

29

この問題を解決しました。解決策は、上記のandメソッドとaUIKeyboardDidShowNotificationおよびUIKeyboardDidHideNotificationオブザーバーの組み合わせです。textFieldDidBeginEditing:textFieldDidEndEditing:

現在選択されている UITextField (activeField と名付けました) を格納する変数、現在のビューが移動されたかどうかを示す変数、キーボードが表示されているかどうかを示す変数の 3 つの追加変数が必要です。

2 つのUITextFieldデリゲート メソッドは次のようになります。

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    activeField = nil;
    // Additional Code
}

ビューが読み込まれると、次の 2 つのオブザーバーが作成されます。

- (void)viewDidLoad {
    // Additional Code
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}

また、対応するメソッドは次のように実装されています。

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardShown )
        return;

    if ( ( activeField != inputAmount ) && ( activeField != inputAge ) ) {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-44;
        frame.size.height += keyboardSize.height-44;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-44;
        frame.size.height -= keyboardSize.height-44;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardShown = NO;
}

このコードは期待どおりに機能するようになりました。キーボードは、[完了] ボタンが押されたときにのみ閉じられます。それ以外の場合、キーボードは表示されたままになり、ビューは移動しません。

animationDuration追加のメモとして、オブジェクトに問い合わせることで動的に取得できると思います。これは、NSNotificationすでに同様のソリューションで遊んだことがありますが、機能していないためです(現在は機能しています)。

于 2009-11-21T16:22:20.417 に答える
2

このView ControllerはUITextViewDelegateである必要があり、設定する必要がありself.textview.delegate = selfますviewdidload

 -(void) textViewDidBeginEditing:(UITextView *)textView
    {
        NSLog(@"%f",self.view.frame.origin.y);
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25f];
        CGRect frame = self.view.frame;
        frame.origin.y =frame.origin.y -204;
        [self.view setFrame:frame];
        [UIView commitAnimations];
    }

-(void) textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + 204;
    [self.view setFrame:frame];
    [UIView commitAnimations];
}
于 2013-07-03T23:47:20.563 に答える
0

UIScrollviewにアウトレットを与えるだけで簡単なことをするだけで問題が発生しました。ビュー内の各テキスト フィールドに固有の Tag プロパティを設定します。

-(void)textFieldDidBeginEditing:(UITextField *)textField
    {   
        switch (textField.tag)
        {
            case 2:    //can be your textfiled tag
              { CGPoint scrollPoint = CGPointMake(0, yourtextfield.frame.origin.y-150); 
                                           //set figure y-150 as per your comfirt
                  [scrollview setContentOffset:scrollPoint animated:YES]; 
             }break;

              case 3:   
              { CGPoint scrollPoint = CGPointMake(0, yourtextfield.frame.origin.y-180); 
                                           //set figure y-180 as per your comfirt
                  [scrollview setContentOffset:scrollPoint animated:YES]; 
             }break;

             ...

         }
    }

    -(void)textFieldDidEndEditing:(UITextField *)textField{

        if(textField.tag==3){
            [scrollview setContentOffset:CGPointZero animated:YES];
                }
         //set the last textfield when you want to disappear keyboard.
    }
于 2013-09-04T05:45:34.797 に答える