Skip to content

Commit 0b20b83

Browse files
authored
Merge pull request slackhq#542 from slackhq/keyboardHeightScroll
Correct content offset for keyboard when uninverted
2 parents 97ff0cd + 5b5301e commit 0b20b83

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Source/SLKTextViewController.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,8 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13801380
[self slk_hideAutoCompletionViewIfNeeded];
13811381
}
13821382

1383+
UIScrollView *scrollView = self.scrollViewProxy;
1384+
13831385
NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
13841386
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
13851387

@@ -1390,10 +1392,10 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13901392
// Scrolls to bottom only if the keyboard is about to show.
13911393
if (self.shouldScrollToBottomAfterKeyboardShows && self.keyboardStatus == SLKKeyboardStatusWillShow) {
13921394
if (self.isInverted) {
1393-
[self.scrollViewProxy slk_scrollToTopAnimated:YES];
1395+
[scrollView slk_scrollToTopAnimated:YES];
13941396
}
13951397
else {
1396-
[self.scrollViewProxy slk_scrollToBottomAnimated:YES];
1398+
[scrollView slk_scrollToBottomAnimated:YES];
13971399
}
13981400
}
13991401
};
@@ -1403,6 +1405,20 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
14031405
// Second condition: check if the height of the keyboard changed.
14041406
if (!CGRectEqualToRect(beginFrame, endFrame) || fabs(previousKeyboardHeight - self.keyboardHC.constant) > 0.0)
14051407
{
1408+
// Content Offset correction if not inverted and not auto-completing.
1409+
if (!self.isInverted && !self.isAutoCompleting) {
1410+
1411+
CGFloat scrollViewHeight = self.scrollViewHC.constant;
1412+
CGFloat keyboardHeight = self.keyboardHC.constant;
1413+
CGSize contentSize = scrollView.contentSize;
1414+
CGPoint contentOffset = scrollView.contentOffset;
1415+
1416+
CGFloat newOffset = MIN(contentSize.height - scrollViewHeight,
1417+
contentOffset.y + keyboardHeight - previousKeyboardHeight);
1418+
1419+
scrollView.contentOffset = CGPointMake(contentOffset.x, newOffset);
1420+
}
1421+
14061422
// 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.
14071423
[self.view slk_animateLayoutIfNeededWithDuration:duration
14081424
bounce:NO

0 commit comments

Comments
 (0)