キーボードが である場合とそうでない場合がある 内のファーストレスポンダーをカバーしているかどうかを確認するにはどうすればUIScrollView
よいUITableView
ですか? UIScrollView
は必ずしも viewController のビュー全体をカバーするわけではなく、モーダル ビュー ( ) に含まれる場合があることに注意してくださいUIModalPresentationFormSheet
。
Apple のリファレンス ドキュメントと exampleからこの修正されたコードを使用していますがCGRectContainsPoint
、キーボードがファーストレスポンダを明らかに覆っている場合でも false を返します。私がconvertRect:toView
正しく使用していないことは明らかです。
また、Apple のコードはビューが全画面表示ではないことを考慮していないため、scrollView の contentInset をキーボードの高さ全体に設定することは優れた解決策ではありません。キーボードを覆う部分に対してのみ挿入する必要があります。最初のレスポンダー。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
// self.scrollView can be a tableView or not. need to handle both
UIView *firstResponderView = [self.scrollView findFirstResponder];
if (!firstResponderView)
return;
NSDictionary* info = [aNotification userInfo];
CGRect rect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// convertRect:toView? convertRect:fromView? Always confusing
CGRect kbRect = [self.scrollView convertRect:rect toView:nil];
CGRect viewRect = [self.scrollView convertRect:firstResponderView.bounds toView:nil];
// doesn't work. convertRect misuse is certainly to blame
if (!CGRectContainsPoint(kbRect, firstResponderView.frame.origin))
return;
// Only inset to the portion which the keyboard covers?
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}