From 7416fb101dfbd66a0b0e250c15fefa978cd6e1aa Mon Sep 17 00:00:00 2001 From: Zheng Liang Date: Thu, 27 Oct 2016 10:49:40 -0700 Subject: [PATCH 1/2] Correct content offset for keyboard when uninverted --- Source/SLKTextViewController.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Source/SLKTextViewController.m b/Source/SLKTextViewController.m index a9cea7b7..ed02622a 100644 --- a/Source/SLKTextViewController.m +++ b/Source/SLKTextViewController.m @@ -1403,6 +1403,16 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification // Second condition: check if the height of the keyboard changed. if (!CGRectEqualToRect(beginFrame, endFrame) || fabs(previousKeyboardHeight - self.keyboardHC.constant) > 0.0) { + // Content Offset correction if not inverted. + if (!self.isInverted) { + // Don't set new offset if autocompletion is showing. + if (self.autoCompletionViewHC.constant == 0 && !self.isAutoCompleting) { + CGFloat newOffset = MIN(self.scrollViewProxy.contentSize.height - self.scrollViewHC.constant, + self.scrollViewProxy.contentOffset.y + self.keyboardHC.constant - previousKeyboardHeight); + self.scrollViewProxy.contentOffset = CGPointMake(self.scrollViewProxy.contentOffset.x, newOffset); + } + } + // Only for this animation, we set bo to bounce since we want to give the impression that the text input is glued to the keyboard. [self.view slk_animateLayoutIfNeededWithDuration:duration bounce:NO From 5b5301eefd403bd0cd81677c6011b8ed256def6b Mon Sep 17 00:00:00 2001 From: Ignacio Romero Zurbuchen Date: Thu, 27 Oct 2016 12:50:58 -0700 Subject: [PATCH 2/2] Small style refactoring --- Source/SLKTextViewController.m | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Source/SLKTextViewController.m b/Source/SLKTextViewController.m index ed02622a..2b8073f8 100644 --- a/Source/SLKTextViewController.m +++ b/Source/SLKTextViewController.m @@ -1380,6 +1380,8 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification [self slk_hideAutoCompletionViewIfNeeded]; } + UIScrollView *scrollView = self.scrollViewProxy; + NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; @@ -1390,10 +1392,10 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification // Scrolls to bottom only if the keyboard is about to show. if (self.shouldScrollToBottomAfterKeyboardShows && self.keyboardStatus == SLKKeyboardStatusWillShow) { if (self.isInverted) { - [self.scrollViewProxy slk_scrollToTopAnimated:YES]; + [scrollView slk_scrollToTopAnimated:YES]; } else { - [self.scrollViewProxy slk_scrollToBottomAnimated:YES]; + [scrollView slk_scrollToBottomAnimated:YES]; } } }; @@ -1403,14 +1405,18 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification // Second condition: check if the height of the keyboard changed. if (!CGRectEqualToRect(beginFrame, endFrame) || fabs(previousKeyboardHeight - self.keyboardHC.constant) > 0.0) { - // Content Offset correction if not inverted. - if (!self.isInverted) { - // Don't set new offset if autocompletion is showing. - if (self.autoCompletionViewHC.constant == 0 && !self.isAutoCompleting) { - CGFloat newOffset = MIN(self.scrollViewProxy.contentSize.height - self.scrollViewHC.constant, - self.scrollViewProxy.contentOffset.y + self.keyboardHC.constant - previousKeyboardHeight); - self.scrollViewProxy.contentOffset = CGPointMake(self.scrollViewProxy.contentOffset.x, newOffset); - } + // Content Offset correction if not inverted and not auto-completing. + if (!self.isInverted && !self.isAutoCompleting) { + + CGFloat scrollViewHeight = self.scrollViewHC.constant; + CGFloat keyboardHeight = self.keyboardHC.constant; + CGSize contentSize = scrollView.contentSize; + CGPoint contentOffset = scrollView.contentOffset; + + CGFloat newOffset = MIN(contentSize.height - scrollViewHeight, + contentOffset.y + keyboardHeight - previousKeyboardHeight); + + scrollView.contentOffset = CGPointMake(contentOffset.x, newOffset); } // Only for this animation, we set bo to bounce since we want to give the impression that the text input is glued to the keyboard.