Skip to content

Commit 0ec0949

Browse files
committed
Merge pull request slackhq#346 from slackhq/keyboard-status-fixes
Keyboard fixes
2 parents 1db201f + b82fb38 commit 0ec0949

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

Source/SLKTextViewController.m

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,12 @@ - (SLKKeyboardStatus)slk_keyboardStatusForNotification:(NSNotification *)notific
495495
return -1;
496496
}
497497

498-
- (BOOL)slk_isIllogicalKeyboardStatus:(SLKKeyboardStatus)status
498+
- (BOOL)slk_isIllogicalKeyboardStatus:(SLKKeyboardStatus)newStatus
499499
{
500-
if ((self.keyboardStatus == 0 && status == 1) ||
501-
(self.keyboardStatus == 1 && status == 2) ||
502-
(self.keyboardStatus == 2 && status == 3) ||
503-
(self.keyboardStatus == 3 && status == 0)) {
500+
if ((self.keyboardStatus == SLKKeyboardStatusDidHide && newStatus == SLKKeyboardStatusWillShow) ||
501+
(self.keyboardStatus == SLKKeyboardStatusWillShow && newStatus == SLKKeyboardStatusDidShow) ||
502+
(self.keyboardStatus == SLKKeyboardStatusDidShow && newStatus == SLKKeyboardStatusWillHide) ||
503+
(self.keyboardStatus == SLKKeyboardStatusWillHide && newStatus == SLKKeyboardStatusDidHide)) {
504504
return NO;
505505
}
506506
return YES;
@@ -915,6 +915,7 @@ - (void)slk_handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
915915
CGFloat keyboardMaxY = CGRectGetHeight(SLKKeyWindowBounds());
916916
CGFloat keyboardMinY = keyboardMaxY - CGRectGetHeight(keyboardView.frame);
917917

918+
918919
// Skips this if it's not the expected textView.
919920
// Checking the keyboard height constant helps to disable the view constraints update on iPad when the keyboard is undocked.
920921
// Checking the keyboard status allows to keep the inputAccessoryView valid when still reacing the bottom of the screen.
@@ -1155,9 +1156,6 @@ - (void)slk_dismissTextInputbarIfNeeded
11551156

11561157
[self slk_hideAutoCompletionViewIfNeeded];
11571158

1158-
// Forces the keyboard status change
1159-
[self slk_updateKeyboardStatus:SLKKeyboardStatusDidHide];
1160-
11611159
[self.view layoutIfNeeded];
11621160
}
11631161

@@ -1307,6 +1305,19 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13071305
return;
13081306
}
13091307

1308+
SLKKeyboardStatus status = [self slk_keyboardStatusForNotification:notification];
1309+
1310+
// Skips if it's the current status
1311+
if (self.keyboardStatus == status) {
1312+
return;
1313+
}
1314+
1315+
// Updates and notifies about the keyboard status update
1316+
if ([self slk_updateKeyboardStatus:status]) {
1317+
// Posts custom keyboard notification, if logical conditions apply
1318+
[self slk_postKeyboarStatusNotification:notification];
1319+
}
1320+
13101321
// Skips this it's not the expected textView and shouldn't force adjustment of the text input bar.
13111322
// This will also dismiss the text input bar if it's visible, and exit auto-completion mode if enabled.
13121323
if (![self.textView isFirstResponder]) {
@@ -1321,13 +1332,6 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13211332
}
13221333
}
13231334

1324-
SLKKeyboardStatus status = [self slk_keyboardStatusForNotification:notification];
1325-
1326-
// Skips if it's the current status
1327-
if (self.keyboardStatus == status) {
1328-
return;
1329-
}
1330-
13311335
// Programatically stops scrolling before updating the view constraints (to avoid scrolling glitch).
13321336
if (status == SLKKeyboardStatusWillShow) {
13331337
[self.scrollViewProxy slk_stopScrolling];
@@ -1342,11 +1346,6 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13421346
self.keyboardHC.constant = [self slk_appropriateKeyboardHeightFromNotification:notification];
13431347
self.scrollViewHC.constant = [self slk_appropriateScrollViewHeight];
13441348

1345-
// Updates and notifies about the keyboard status update
1346-
if ([self slk_updateKeyboardStatus:status]) {
1347-
// Posts custom keyboard notification, if logical conditions apply
1348-
[self slk_postKeyboarStatusNotification:notification];
1349-
}
13501349

13511350
NSInteger curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
13521351
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
@@ -1406,6 +1405,12 @@ - (void)slk_didShowOrHideKeyboard:(NSNotification *)notification
14061405
return;
14071406
}
14081407

1408+
// Updates and notifies about the keyboard status update
1409+
if ([self slk_updateKeyboardStatus:status]) {
1410+
// Posts custom keyboard notification, if logical conditions apply
1411+
[self slk_postKeyboarStatusNotification:notification];
1412+
}
1413+
14091414
// After showing keyboard, check if the current cursor position could diplay autocompletion
14101415
if ([self.textView isFirstResponder] && status == SLKKeyboardStatusDidShow && !self.isAutoCompleting) {
14111416

@@ -1415,12 +1420,6 @@ - (void)slk_didShowOrHideKeyboard:(NSNotification *)notification
14151420
});
14161421
}
14171422

1418-
// Updates and notifies about the keyboard status update
1419-
if ([self slk_updateKeyboardStatus:status]) {
1420-
// Posts custom keyboard notification, if logical conditions apply
1421-
[self slk_postKeyboarStatusNotification:notification];
1422-
}
1423-
14241423
// Very important to invalidate this flag after the keyboard is dismissed or presented, to start with a clean state next time.
14251424
self.movingKeyboard = NO;
14261425
}

Source/UIScrollView+SLKAdditions.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,24 @@ @implementation UIScrollView (SLKAdditions)
2020

2121
- (void)slk_scrollToTopAnimated:(BOOL)animated
2222
{
23-
[self setContentOffset:CGPointZero animated:animated];
23+
if ([self slk_canScroll]) {
24+
[self setContentOffset:CGPointZero animated:animated];
25+
}
2426
}
2527

2628
- (void)slk_scrollToBottomAnimated:(BOOL)animated
2729
{
28-
[self setContentOffset:[self slk_bottomRect].origin animated:animated];
30+
if ([self slk_canScroll]) {
31+
[self setContentOffset:[self slk_bottomRect].origin animated:animated];
32+
}
33+
}
34+
35+
- (BOOL)slk_canScroll
36+
{
37+
if (self.contentSize.height > CGRectGetHeight(self.frame)) {
38+
return YES;
39+
}
40+
return NO;
2941
}
3042

3143
- (BOOL)slk_isAtTop

0 commit comments

Comments
 (0)