eminFirStresponder:最初に応答しているUIControlに自動的にスクロールする
-
15-11-2019 - |
質問
私は1つのセクションと多くの行を持つグループ化されたUITableViewController
を持っています。各cell
は、説明を持つUILabel
と入力用のUITextField
で構成されています。フォーム、私は言うでしょう。
下部には、次のビューへの移動が「次へ」ボタンです。しかし、それ以前はUITextField
を検証します。ユーザーがフィールドを入力していない場合、このフィールドはフォーカスを取得する必要があるため、ユーザーは何かを入力する必要があることを確認します。私はこれで試してみました:
[inputField becomeFirstResponder];
.
覚えておいて、私のビューのボタンの下のはです。この状況では、フィールドは遠くすぎるのでフォーカスを受けません。ゆっくりスクロールしてフィールドが表示されたら、フィールドは最初のレスポンダになり、カーソルを取得します。私の指でスクロールしたくない、フィールドはフォーカスを自動的に入手する必要があります。
私の分野に直接「ジャンプ」する方法はありますか? UITextField
に代わるものを使用してください。自動的に自分のフィールドまでスクロールしますか?
あなたの助けをありがとう!
解決
If you know the index of cell with the UITextField you want to edit, use
NSIndexPath* indexPath = [[NSIndexPath indexPathWithIndex:your_section_index] indexPathByAddingIndex:your_cell_index];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]
before you call becomeFirstResponder
.
If the cell is off the screen, becomeFirstResponder may fail for some versions of iOS. In addition, some versions will perform additional scrolling on the tableview in some situations (e.g. if Apple's code determines that the responder would still be off the screen without more scrolling). To get around these issues, you may need to call become first responder after the scroll animation is complete.
他のヒント
I'd the same issue. it is due to the UIAnimation occurring for the two tasks simultaneously. So while the UIAnimation for scrollToRowAtIndexPath ignores the becomeFirstResponder .
The solution might be disable the Animation effect one of it OR else using the performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:1.0f];
.