Skip to content

Commit 91e517d

Browse files
committed
Fixed the window offset on iOS when resuming an app with a borderless or fullscreen window that has the on-screen keyboard visible.
1 parent 0187428 commit 91e517d

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

src/video/uikit/SDL_uikitviewcontroller.m

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -212,28 +212,12 @@ - (void)hideKeyboard
212212
- (void)keyboardWillShow:(NSNotification *)notification
213213
{
214214
CGRect kbrect = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
215-
UIView *view = self.view;
216-
int height = 0;
217-
218-
/* The keyboard rect is in the coordinate space of the screen, but we want
219-
* its height in the view's coordinate space. */
220-
#ifdef __IPHONE_8_0
221-
if ([view respondsToSelector:@selector(convertRect:fromCoordinateSpace:)]) {
222-
UIScreen *screen = view.window.screen;
223-
kbrect = [view convertRect:kbrect fromCoordinateSpace:screen.coordinateSpace];
224-
height = kbrect.size.height;
225-
} else
226-
#endif
227-
{
228-
/* In iOS 7 and below, the screen's coordinate space is never rotated. */
229-
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
230-
height = kbrect.size.width;
231-
} else {
232-
height = kbrect.size.height;
233-
}
234-
}
235215

236-
[self setKeyboardHeight:height];
216+
/* The keyboard rect is in the coordinate space of the screen/window, but we
217+
* want its height in the coordinate space of the view. */
218+
kbrect = [self.view convertRect:kbrect fromView:nil];
219+
220+
[self setKeyboardHeight:(int)kbrect.size.height];
237221
}
238222

239223
- (void)keyboardWillHide:(NSNotification *)notification
@@ -243,28 +227,29 @@ - (void)keyboardWillHide:(NSNotification *)notification
243227

244228
- (void)updateKeyboard
245229
{
246-
SDL_Rect textrect = self.textInputRect;
247230
CGAffineTransform t = self.view.transform;
248231
CGPoint offset = CGPointMake(0.0, 0.0);
232+
CGRect frame = UIKit_ComputeViewFrame(window, self.view.window.screen);
249233

250234
if (self.keyboardHeight) {
251-
int rectbottom = textrect.y + textrect.h;
252-
int kbottom = self.view.bounds.size.height - self.keyboardHeight;
253-
if (kbottom < rectbottom) {
254-
offset.y = kbottom - rectbottom;
235+
int rectbottom = self.textInputRect.y + self.textInputRect.h;
236+
int keybottom = self.view.bounds.size.height - self.keyboardHeight;
237+
if (keybottom < rectbottom) {
238+
offset.y = keybottom - rectbottom;
255239
}
256240
}
257241

258-
/* Put the offset into the this view transform's coordinate space. */
242+
/* Apply this view's transform (except any translation) to the offset, in
243+
* order to orient it correctly relative to the frame's coordinate space. */
259244
t.tx = 0.0;
260245
t.ty = 0.0;
261246
offset = CGPointApplyAffineTransform(offset, t);
262247

263-
t.tx = offset.x;
264-
t.ty = offset.y;
248+
/* Apply the updated offset to the view's frame. */
249+
frame.origin.x += offset.x;
250+
frame.origin.y += offset.y;
265251

266-
/* Move the view by applying the updated transform. */
267-
self.view.transform = t;
252+
self.view.frame = frame;
268253
}
269254

270255
- (void)setKeyboardHeight:(int)height

src/video/uikit/SDL_uikitwindow.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
273273

274274
/* Update the view's frame to account for the status bar change. */
275275
viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
276+
277+
#ifdef SDL_IPHONE_KEYBOARD
278+
/* Make sure the view is offset correctly when the keyboard is visible. */
279+
[viewcontroller updateKeyboard];
280+
#endif
281+
276282
[viewcontroller.view setNeedsLayout];
277283
[viewcontroller.view layoutIfNeeded];
278284
}

0 commit comments

Comments
 (0)