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

Commit

Permalink
Merge pull request #542 from slackhq/keyboardHeightScroll
Browse files Browse the repository at this point in the history
Correct content offset for keyboard when uninverted
  • Loading branch information
ZAndyL authored Oct 27, 2016
2 parents 97ff0cd + 5b5301e commit 0b20b83
Showing 1 changed file with 18 additions and 2 deletions.
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

0 comments on commit 0b20b83

Please sign in to comment.