Fixed the window offset on iOS when resuming an app with a borderless or fullscreen window that has the on-screen keyboard visible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index af1f68d..7ebc117 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -212,28 +212,12 @@
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect kbrect = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
- UIView *view = self.view;
- int height = 0;
-
- /* The keyboard rect is in the coordinate space of the screen, but we want
- * its height in the view's coordinate space. */
-#ifdef __IPHONE_8_0
- if ([view respondsToSelector:@selector(convertRect:fromCoordinateSpace:)]) {
- UIScreen *screen = view.window.screen;
- kbrect = [view convertRect:kbrect fromCoordinateSpace:screen.coordinateSpace];
- height = kbrect.size.height;
- } else
-#endif
- {
- /* In iOS 7 and below, the screen's coordinate space is never rotated. */
- if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
- height = kbrect.size.width;
- } else {
- height = kbrect.size.height;
- }
- }
- [self setKeyboardHeight:height];
+ /* The keyboard rect is in the coordinate space of the screen/window, but we
+ * want its height in the coordinate space of the view. */
+ kbrect = [self.view convertRect:kbrect fromView:nil];
+
+ [self setKeyboardHeight:(int)kbrect.size.height];
}
- (void)keyboardWillHide:(NSNotification *)notification
@@ -243,28 +227,29 @@
- (void)updateKeyboard
{
- SDL_Rect textrect = self.textInputRect;
CGAffineTransform t = self.view.transform;
CGPoint offset = CGPointMake(0.0, 0.0);
+ CGRect frame = UIKit_ComputeViewFrame(window, self.view.window.screen);
if (self.keyboardHeight) {
- int rectbottom = textrect.y + textrect.h;
- int kbottom = self.view.bounds.size.height - self.keyboardHeight;
- if (kbottom < rectbottom) {
- offset.y = kbottom - rectbottom;
+ int rectbottom = self.textInputRect.y + self.textInputRect.h;
+ int keybottom = self.view.bounds.size.height - self.keyboardHeight;
+ if (keybottom < rectbottom) {
+ offset.y = keybottom - rectbottom;
}
}
- /* Put the offset into the this view transform's coordinate space. */
+ /* Apply this view's transform (except any translation) to the offset, in
+ * order to orient it correctly relative to the frame's coordinate space. */
t.tx = 0.0;
t.ty = 0.0;
offset = CGPointApplyAffineTransform(offset, t);
- t.tx = offset.x;
- t.ty = offset.y;
+ /* Apply the updated offset to the view's frame. */
+ frame.origin.x += offset.x;
+ frame.origin.y += offset.y;
- /* Move the view by applying the updated transform. */
- self.view.transform = t;
+ self.view.frame = frame;
}
- (void)setKeyboardHeight:(int)height
diff --git a/src/video/uikit/SDL_uikitwindow.m b/src/video/uikit/SDL_uikitwindow.m
index 4010ca6..8647fce 100644
--- a/src/video/uikit/SDL_uikitwindow.m
+++ b/src/video/uikit/SDL_uikitwindow.m
@@ -273,6 +273,12 @@ UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
/* Update the view's frame to account for the status bar change. */
viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
+
+#ifdef SDL_IPHONE_KEYBOARD
+ /* Make sure the view is offset correctly when the keyboard is visible. */
+ [viewcontroller updateKeyboard];
+#endif
+
[viewcontroller.view setNeedsLayout];
[viewcontroller.view layoutIfNeeded];
}