Skip to content

Commit 7e84c0e

Browse files
committed
fixed issue where input bar only appeared behind keyboard (slackhq#605, slackhq#615)
1 parent e1604dd commit 7e84c0e

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

Source/SLKTextViewController.m

+40-21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ @interface SLKTextViewController ()
2828
{
2929
CGPoint _scrollViewOffsetBeforeDragging;
3030
CGFloat _keyboardHeightBeforeDragging;
31+
CGRect _cachedKeyboardRect;
3132
}
3233

3334
// The shared scrollView pointer, either a tableView or collectionView
@@ -85,7 +86,7 @@ - (instancetype)initWithTableViewStyle:(UITableViewStyle)style
8586
{
8687
NSAssert([self class] != [SLKTextViewController class], @"Oops! You must subclass SLKTextViewController.");
8788
NSAssert(style == UITableViewStylePlain || style == UITableViewStyleGrouped, @"Oops! You must pass a valid UITableViewStyle.");
88-
89+
8990
if (self = [super initWithNibName:nil bundle:nil])
9091
{
9192
self.scrollViewProxy = [self tableViewWithStyle:style];
@@ -98,7 +99,7 @@ - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
9899
{
99100
NSAssert([self class] != [SLKTextViewController class], @"Oops! You must subclass SLKTextViewController.");
100101
NSAssert([layout isKindOfClass:[UICollectionViewLayout class]], @"Oops! You must pass a valid UICollectionViewLayout object.");
101-
102+
102103
if (self = [super initWithNibName:nil bundle:nil])
103104
{
104105
self.scrollViewProxy = [self collectionViewWithLayout:layout];
@@ -111,7 +112,7 @@ - (instancetype)initWithScrollView:(UIScrollView *)scrollView
111112
{
112113
NSAssert([self class] != [SLKTextViewController class], @"Oops! You must subclass SLKTextViewController.");
113114
NSAssert([scrollView isKindOfClass:[UIScrollView class]], @"Oops! You must pass a valid UIScrollView object.");
114-
115+
115116
if (self = [super initWithNibName:nil bundle:nil])
116117
{
117118
_scrollView = scrollView;
@@ -127,7 +128,7 @@ - (instancetype)initWithCoder:(NSCoder *)decoder
127128
{
128129
NSAssert([self class] != [SLKTextViewController class], @"Oops! You must subclass SLKTextViewController.");
129130
NSAssert([decoder isKindOfClass:[NSCoder class]], @"Oops! You must pass a valid decoder object.");
130-
131+
131132
if (self = [super initWithCoder:decoder])
132133
{
133134
UITableViewStyle tableViewStyle = [[self class] tableViewStyleForCoder:decoder];
@@ -158,6 +159,8 @@ - (void)slk_commonInit
158159

159160
self.automaticallyAdjustsScrollViewInsets = YES;
160161
self.extendedLayoutIncludesOpaqueBars = YES;
162+
163+
_cachedKeyboardRect = CGRectNull;
161164
}
162165

163166

@@ -241,7 +244,7 @@ - (void)viewSafeAreaInsetsDidChange
241244
{
242245
[super viewSafeAreaInsetsDidChange];
243246

244-
[self slk_updateViewConstraints];
247+
[self slk_updateViewConstraintsFromCachedKeyboard];
245248
}
246249

247250

@@ -266,7 +269,7 @@ - (UITableView *)tableViewWithStyle:(UITableViewStyle)style
266269
_tableView.dataSource = self;
267270
_tableView.delegate = self;
268271
_tableView.clipsToBounds = NO;
269-
272+
270273
[self slk_updateInsetAdjustmentBehavior];
271274
}
272275
return _tableView;
@@ -397,9 +400,9 @@ - (CGFloat)slk_appropriateKeyboardHeightFromNotification:(NSNotification *)notif
397400
return [self slk_appropriateBottomMargin];
398401
}
399402

400-
CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
403+
_cachedKeyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
401404

402-
return [self slk_appropriateKeyboardHeightFromRect:keyboardRect];
405+
return [self slk_appropriateKeyboardHeightFromRect:_cachedKeyboardRect];
403406
}
404407

405408
- (CGFloat)slk_appropriateKeyboardHeightFromRect:(CGRect)rect
@@ -588,16 +591,16 @@ - (void)setBounces:(BOOL)bounces
588591
}
589592

590593
- (void)slk_updateInsetAdjustmentBehavior
591-
{
592-
// Deactivate automatic scrollView adjustment for inverted table view
593-
if (@available(iOS 11.0, *)) {
594-
if (self.isInverted) {
595-
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
596-
} else {
597-
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
598-
}
594+
{
595+
// Deactivate automatic scrollView adjustment for inverted table view
596+
if (@available(iOS 11.0, *)) {
597+
if (self.isInverted) {
598+
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
599+
} else {
600+
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
599601
}
600602
}
603+
}
601604

602605
- (BOOL)slk_updateKeyboardStatus:(SLKKeyboardStatus)status
603606
{
@@ -906,7 +909,7 @@ - (void)setTextInputbarHidden:(BOOL)hidden animated:(BOOL)animated
906909
}
907910

908911
_textInputbar.hidden = hidden;
909-
912+
910913
if (@available(iOS 11.0, *)) {
911914
[self viewSafeAreaInsetsDidChange];
912915
}
@@ -1196,7 +1199,7 @@ - (void)slk_enableTypingSuggestionIfNeeded
11961199
BOOL enable = !self.isAutoCompleting;
11971200

11981201
NSString *inputPrimaryLanguage = self.textView.textInputMode.primaryLanguage;
1199-
1202+
12001203
// Toggling autocorrect on Japanese keyboards breaks autocompletion by replacing the autocompletion prefix by an empty string.
12011204
// So for now, let's not disable autocorrection for Japanese.
12021205
if ([inputPrimaryLanguage isEqualToString:@"ja-JP"]) {
@@ -1345,7 +1348,7 @@ - (void)didPressEscapeKey:(UIKeyCommand *)keyCommand
13451348
else if (_textInputbar.isEditing) {
13461349
[self didCancelTextEditing:keyCommand];
13471350
}
1348-
1351+
13491352
CGFloat bottomMargin = [self slk_appropriateBottomMargin];
13501353

13511354
if ([self ignoreTextInputbarAdjustment] || ([self.textView isFirstResponder] && self.keyboardHC.constant == bottomMargin)) {
@@ -1399,6 +1402,8 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
13991402
// Programatically stops scrolling before updating the view constraints (to avoid scrolling glitch).
14001403
if (status == SLKKeyboardStatusWillShow) {
14011404
[self.scrollViewProxy slk_stopScrolling];
1405+
} else if (status == SLKKeyboardStatusWillHide) {
1406+
_cachedKeyboardRect = CGRectNull;
14021407
}
14031408

14041409
// Stores the previous keyboard height
@@ -1478,6 +1483,7 @@ - (void)slk_didShowOrHideKeyboard:(NSNotification *)notification
14781483
if (!self.isViewVisible) {
14791484
if (status == SLKKeyboardStatusDidHide && self.keyboardStatus == SLKKeyboardStatusWillHide) {
14801485
// Even if the view isn't visible anymore, let's still continue to update all states.
1486+
_cachedKeyboardRect = CGRectNull;
14811487
}
14821488
else {
14831489
return;
@@ -2287,13 +2293,25 @@ - (void)slk_updateViewConstraints
22872293
[super updateViewConstraints];
22882294
}
22892295

2296+
- (void)slk_updateViewConstraintsFromCachedKeyboard
2297+
{
2298+
self.textInputbarHC.constant = self.textInputbar.minimumInputbarHeight;
2299+
self.scrollViewHC.constant = [self slk_appropriateScrollViewHeight];
2300+
self.keyboardHC.constant = [self slk_appropriateKeyboardHeightFromRect:_cachedKeyboardRect];
2301+
2302+
if (_textInputbar.isEditing) {
2303+
self.textInputbarHC.constant += self.textInputbar.editorContentViewHeight;
2304+
}
2305+
2306+
[super updateViewConstraints];
2307+
}
22902308

22912309
#pragma mark - Keyboard Command registration
22922310

22932311
- (void)slk_registerKeyCommands
22942312
{
22952313
__weak typeof(self) weakSelf = self;
2296-
2314+
22972315
// Enter Key
22982316
[self.textView observeKeyInput:@"\r" modifiers:0 title:NSLocalizedString(@"Send/Accept", nil) completion:^(UIKeyCommand *keyCommand) {
22992317
[weakSelf didPressReturnKey:keyCommand];
@@ -2360,7 +2378,7 @@ - (void)slk_registerNotifications
23602378
- (void)slk_unregisterNotifications
23612379
{
23622380
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
2363-
2381+
23642382
// Keyboard notifications
23652383
[notificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
23662384
[notificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
@@ -2446,3 +2464,4 @@ - (void)dealloc
24462464
}
24472465

24482466
@end
2467+

0 commit comments

Comments
 (0)