michaeltyson / TPKeyboardAvoiding

A drop-in universal solution for moving text fields out of the way of the keyboard in iOS
http://atastypixel.com/blog/a-drop-in-universal-solution-for-moving-text-fields-out-of-the-way-of-the-keyboard/
zlib License
5.81k stars 925 forks source link

does not work with custom keyboard #130

Open edward-s opened 9 years ago

edward-s commented 9 years ago

it seems like if I use iOS 8.0 custom keyboard i.e: swiftkey, flesky

the scrollview does not adjust properly

further debugging indicates that state.keyboardRect.height is always 0 when using custom keyboard

any idea how to fix this?

PWani commented 9 years ago

Seeing this with SwiftKey.

sebinsua commented 9 years ago

Same. Anybody have any idea how to fix?

crivera commented 9 years ago

you can fix it by doing the following:

in the Keyboard_willShow method add this:

viewableHeight = MIN(352, viewableHeight); // 352 is the height of the secure keyboard so make it at least that tall (probably better way to calculate it)

also in the Keyboard_willHide add all the way at the end:

[self setContentOffset:CGPointMake(0,0)];

this works for me

dcty commented 9 years ago

@implementation TPKeyboardAvoidingState

override keyboardVisible getter can fix the problem

jdevuyst commented 9 years ago

I'm not sure why it works (not having looked at this in detail), but the code that @dcty posted work very well for me. @dcty, do you intend to make a pull request?

johannwerner commented 9 years ago

@dcty fix doesn't work well for me. When I dismiss keyboard my buttons at the bottom of the screen are off the screen because the state.priorScrollIndicatorInsets and state.priorInset don't get reset to correct values.

This should fix both issues.

- (void)TPKeyboardAvoiding_keyboardWillShow:(NSNotification*)notification {
    TPKeyboardAvoidingState *state = self.keyboardAvoidingState;

    if ( state.keyboardVisible ) {
        return;
    }

    UIView *firstResponder = [self TPKeyboardAvoiding_findFirstResponderBeneathView:self];

    state.keyboardRect = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
    if (state.keyboardRect.size.height != 0) {
        state.keyboardVisible = YES;
        state.priorInset = self.contentInset;
        state.priorScrollIndicatorInsets = self.scrollIndicatorInsets;
        if ( [self isKindOfClass:[TPKeyboardAvoidingScrollView class]] ) {
            state.priorContentSize = self.contentSize;

            if ( CGSizeEqualToSize(self.contentSize, CGSizeZero) ) {
                // Set the content size, if it's not set. Do not set content size explicitly if auto-layout
                // is being used to manage subviews
                self.contentSize = [self TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames];
            }
        }

        // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
        [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];

        self.contentInset = [self TPKeyboardAvoiding_contentInsetForKeyboard];

        if ( firstResponder ) {
            CGFloat viewableHeight = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom;
            [self setContentOffset:CGPointMake(self.contentOffset.x,
                                               [self TPKeyboardAvoiding_idealOffsetForView:firstResponder
                                                                     withViewingAreaHeight:viewableHeight])
                          animated:NO];
        }

        self.scrollIndicatorInsets = self.contentInset;

        [UIView commitAnimations];

    }
}