我有一个显示带有 UITextView 的视图的视图控制器,并且我想在键盘出现时调整视图的大小,以便 UITextView 不会被键盘覆盖。我几乎在所有情况下都能正常工作。据我所知,只有当视图控制器以 ModalPresentationStyleFormSheet 呈现并且仅以 LandscapeRight 方向呈现时,我仍然在 iPad 上看到了一些奇怪现象。
我的视图控制器 -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 获得的原点比它应该的要低很多。这可能是什么原因造成的?
感谢您的帮助!