私は、ビューの上部からビューの下部までのテキストフィールドを持つビューを持つアプリケーションを持っています。フィールドが表示されるようにボトムフィールドを編集するときにスクロールする必要がありましたが、正しく動作するようには見えませんでした。アクティブなテキストフィールドでのキーボードスクロール - 非表示へのスクロール?
Apple docsに続いて、そのコードをすべて私のプログラム(Listings 4-1,4-2)に置き、scrollView
とactiveField
アウトレットをヘッダファイルに追加してIBにリンクしました。
問題は、テキストフィールドをクリックすると、すべてのテキストフィールドが表示されなくなり、キーボードを閉じるまで問題になります。それらは非常に遠くまでスクロールします(再び、フィールドが表示されていない場所まで十分に移動します)。
この問題が原因で発生する可能性のあることは誰でも知っていますか?
私はクリックしなくても使用しているコードを正確に見ることができるように、ここにApple Docsのコードを配置します。
//my .h
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *activeField;
//.m
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
それはので、それを再現するために、アップルから許可を持っていることは疑問だので、私はあなたのポストのうち、アップルのコードのコピー&ペーストを編集しました。私はまた、司会者に見せるようにと指示しました - 私は公式の方針が何であるべきか、そうすべきかどうかはわかりません。 –
@ジョシュ: "フェアユース"(http://en.wikipedia.org/wiki/Fair_use)の目的のために、コードの量はおそらくOKです。 –
@Robert:ありがとう、ありがとう。ジェームズ、私はそれがあなたがそう選ぶなら、あなたがそれを戻すべきであることを意味すると思います。気にして申し訳ありません。 –