4

UITextView を使用してビューを表示するビュー コントローラーがあり、キーボードが表示されたときにビューのサイズを変更して、UITextView がキーボードで覆われないようにしたいと考えています。ほとんどすべての場合、これは正しく機能しています。私が知る限り、View ControllerがModalPresentationStyleFormSheetで表示され、LandscapeRightの向きでのみ表示されている場合にのみ、iPadでまだ奇妙な点が見られます。

私のView Controllerの-keyboardWillShowの関連部分:

// We'll store my frame above the keyboard in availableFrame
CGRect availableFrame = self.view.frame;

// Find the keyboard size
NSDictionary *userInfo = [notification userInfo];
NSValue keyboardFrameScreenValue = userInfo[UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameScreen =  [keyboardFrameScreenValue CGRectValue];
CGRect keyboardFrame = [self.view convertRect:keyboardFrameScreen fromView:nil];
CGSize keyboardSize = keyboardFrame.size;

// Figure out how much of my frame is covered by the keyboard
CGRect screenBounds = [self.view convertRect:[UIScreen mainScreen].bounds
                                    fromView:nil];
CGRect myBoundsScreen = [self.view boundsInWindow]; // See below
CGFloat myBottom = myBoundsScreen.origin.y + myBoundsScreen.size.height;
CGFloat keyboardTop = screenBounds.size.height - keyboardSize.height;
CGFloat lostHeight = myBottom - keyboardTop;
availableFrame.size.height -= lostHeight;

-[UIView boundsInWindow]:

- (CGRect)boundsInWindow {
  UIInterfaceOrientation orientation =
    [UIApplication sharedApplication].statusBarOrientation;
  CGRect bounds = [self convertRect:self.bounds toView:self.window];
  if (UIInterfaceOrientationIsLandscape(orientation)) {
    // Swap origin
    CGFloat x = bounds.origin.y;
    bounds.origin.y = bounds.origin.x;
    bounds.origin.x = x;
    // Swap size
    CGFloat width = bounds.size.height;
    bounds.size.height = bounds.size.width;
    bounds.size.width = width;
  }

  return bounds;
}

ほとんどの場合、これは機能します。私のアプリがユーザー インターフェースの向きが LandscapeRight の場合、-boundsInWindow から得られる原点は本来あるべき位置よりもかなり低くなります。何が原因でしょうか?

ご協力ありがとうございます。

4

3 に答える 3