Gre*_*reg 4 keyboard screen-rotation modalviewcontroller ios uimodalpresentationstyle
我有一个视图控制器显示一个带有 UITextView 的视图,我想在键盘出现时调整视图的大小,以便键盘不会覆盖 UITextView。我几乎在所有情况下都能正常工作。据我所知,我仍然在 iPad 上看到一些奇怪的东西,只有当视图控制器出现在 ModalPresentationStyleFormSheet 中时,而且只有在 LandscapeRight 方向上。
我的视图控制器的 -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;
Run Code Online (Sandbox Code Playgroud)
-[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;
}
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都有效。但是,当我的应用程序处于用户界面方向 LandscapeRight 时,我从 -boundsInWindow 获得的原点比应有的要低很多。什么可能导致这种情况?
感谢您的任何帮助!
威尔的回答是正确的想法,但我不得不对其进行一些调整以使其正确
extension UIView {
func heightCoveredByKeyboardOfSize(keyboardSize: CGSize) -> CGFloat {
let frameInWindow = convertRect(bounds, toView: nil)
guard let windowBounds = window?.bounds else { return 0 }
let keyboardTop = windowBounds.size.height - keyboardSize.height
let viewBottom = frameInWindow.origin.y + frameInWindow.size.height
return max(0, viewBottom - keyboardTop)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |