UITextFieldまたはUITextViewのキーボードが表示されるときのインターフェイスの調整

StackOverflow https://stackoverflow.com/questions/402658

  •  03-07-2019
  •  | 
  •  

質問

各セルにラベルとテキストフィールドを含むテーブルがあります。問題は、最後の行を編集するときに、キーボードがテーブルの下部を非表示にし、入力内容が表示されないことです。キーボードの上にインターフェイスを移動して、入力内容を確認するにはどうすればよいですか?

ありがとう、 ムスタファ

役に立ちましたか?

解決

UIKeyboardDidShowNotification および UIKeyboardWillHideNotification イベントのviewControllerを登録する必要があります。これらを取得したら、テーブルの境界を調整する必要があります。キーボードの高さは170ピクセルなので、テーブルの境界を適切に縮小または拡大するだけで、キーボードに合わせて適切に調整する必要があります。

他のヒント

この問題は、UIシナリオによって複雑です。ここでは、UITextFieldまたはUITextviewがUITableViewCellにあるシナリオについて説明します。

  1. NSNotificationCenterを使用して、UIKeyboardDidShowNotificationイベントを検出する必要があります。 http://iosdevelopertips.com/user-interface/adjustを参照してください。 -textfield-hidden-by-keyboard.html 。 UITableViewフレームサイズを縮小して、キーボードで覆われていない画面領域のみを占めるようにする必要があります。

    1. UITableViewCellをタップすると、OSはセルをUITableViewの表示領域内に自動的に配置します。ただし、UITableViewCellに存在する場合でも、UITextViewまたはUITableViewCellをタップすると発生しません。

電話する必要があります

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

プログラムで「タップ」するにはセル。

両方のポイントを実装すると、キーボードのすぐ上にUITextView / Fieldの位置が表示されます。 UITableView / Fieldが存在するUITableViewCellの高さは、「覆われていない」ものより高くできないことに注意してください。エリア。これが当てはまらない場合は、別のアプローチがありますが、ここでは説明しません。

その下に十分なスクロールスペースがあることを確認してください。私の知る限り、iPhoneはキーボードが表示されたときに自動的にフォーカスされたテキストボックスを調整して表示するためです。

長方形の高さが変更されている行を削除/コメントすると、問題が解決するようです。ありがとう。

変更されたコード:

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

}

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

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

    if( activeTextField != MoneyCollected)
    {
        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-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = 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-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

このコーディングを試してくださいこれはあなたのために役立ちます

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top