Fixed the window offset on iOS when resuming an app with a borderless or fullscreen window that has the on-screen keyboard visible.
authorAlex Szpakowski <slime73@gmail.com>
Tue, 05 May 2015 16:20:11 -0300
changeset 95858339700453c6
parent 9584 fceb5503cadf
child 9586 037074526d4c
Fixed the window offset on iOS when resuming an app with a borderless or fullscreen window that has the on-screen keyboard visible.
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/src/video/uikit/SDL_uikitviewcontroller.m	Tue May 05 16:16:10 2015 -0300
     1.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Tue May 05 16:20:11 2015 -0300
     1.3 @@ -212,28 +212,12 @@
     1.4  - (void)keyboardWillShow:(NSNotification *)notification
     1.5  {
     1.6      CGRect kbrect = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
     1.7 -    UIView *view = self.view;
     1.8 -    int height = 0;
     1.9  
    1.10 -    /* The keyboard rect is in the coordinate space of the screen, but we want
    1.11 -     * its height in the view's coordinate space. */
    1.12 -#ifdef __IPHONE_8_0
    1.13 -    if ([view respondsToSelector:@selector(convertRect:fromCoordinateSpace:)]) {
    1.14 -        UIScreen *screen = view.window.screen;
    1.15 -        kbrect = [view convertRect:kbrect fromCoordinateSpace:screen.coordinateSpace];
    1.16 -        height = kbrect.size.height;
    1.17 -    } else
    1.18 -#endif
    1.19 -    {
    1.20 -        /* In iOS 7 and below, the screen's coordinate space is never rotated. */
    1.21 -        if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    1.22 -            height = kbrect.size.width;
    1.23 -        } else {
    1.24 -            height = kbrect.size.height;
    1.25 -        }
    1.26 -    }
    1.27 +    /* The keyboard rect is in the coordinate space of the screen/window, but we
    1.28 +     * want its height in the coordinate space of the view. */
    1.29 +    kbrect = [self.view convertRect:kbrect fromView:nil];
    1.30  
    1.31 -    [self setKeyboardHeight:height];
    1.32 +    [self setKeyboardHeight:(int)kbrect.size.height];
    1.33  }
    1.34  
    1.35  - (void)keyboardWillHide:(NSNotification *)notification
    1.36 @@ -243,28 +227,29 @@
    1.37  
    1.38  - (void)updateKeyboard
    1.39  {
    1.40 -    SDL_Rect textrect = self.textInputRect;
    1.41      CGAffineTransform t = self.view.transform;
    1.42      CGPoint offset = CGPointMake(0.0, 0.0);
    1.43 +    CGRect frame = UIKit_ComputeViewFrame(window, self.view.window.screen);
    1.44  
    1.45      if (self.keyboardHeight) {
    1.46 -        int rectbottom = textrect.y + textrect.h;
    1.47 -        int kbottom = self.view.bounds.size.height - self.keyboardHeight;
    1.48 -        if (kbottom < rectbottom) {
    1.49 -            offset.y = kbottom - rectbottom;
    1.50 +        int rectbottom = self.textInputRect.y + self.textInputRect.h;
    1.51 +        int keybottom = self.view.bounds.size.height - self.keyboardHeight;
    1.52 +        if (keybottom < rectbottom) {
    1.53 +            offset.y = keybottom - rectbottom;
    1.54          }
    1.55      }
    1.56  
    1.57 -    /* Put the offset into the this view transform's coordinate space. */
    1.58 +    /* Apply this view's transform (except any translation) to the offset, in
    1.59 +     * order to orient it correctly relative to the frame's coordinate space. */
    1.60      t.tx = 0.0;
    1.61      t.ty = 0.0;
    1.62      offset = CGPointApplyAffineTransform(offset, t);
    1.63  
    1.64 -    t.tx = offset.x;
    1.65 -    t.ty = offset.y;
    1.66 +    /* Apply the updated offset to the view's frame. */
    1.67 +    frame.origin.x += offset.x;
    1.68 +    frame.origin.y += offset.y;
    1.69  
    1.70 -    /* Move the view by applying the updated transform. */
    1.71 -    self.view.transform = t;
    1.72 +    self.view.frame = frame;
    1.73  }
    1.74  
    1.75  - (void)setKeyboardHeight:(int)height
     2.1 --- a/src/video/uikit/SDL_uikitwindow.m	Tue May 05 16:16:10 2015 -0300
     2.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Tue May 05 16:20:11 2015 -0300
     2.3 @@ -273,6 +273,12 @@
     2.4  
     2.5      /* Update the view's frame to account for the status bar change. */
     2.6      viewcontroller.view.frame = UIKit_ComputeViewFrame(window, data.uiwindow.screen);
     2.7 +
     2.8 +#ifdef SDL_IPHONE_KEYBOARD
     2.9 +    /* Make sure the view is offset correctly when the keyboard is visible. */
    2.10 +    [viewcontroller updateKeyboard];
    2.11 +#endif
    2.12 +
    2.13      [viewcontroller.view setNeedsLayout];
    2.14      [viewcontroller.view layoutIfNeeded];
    2.15  }