Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Correct content offset for keyboard when uninverted #542

Merged
merged 2 commits into from
Oct 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Source/SLKTextViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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];
}
}
};
Expand All @@ -1403,6 +1405,20 @@ - (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 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.
[self.view slk_animateLayoutIfNeededWithDuration:duration
bounce:NO
Expand Down