@@ -28,6 +28,7 @@ @interface SLKTextViewController ()
28
28
{
29
29
CGPoint _scrollViewOffsetBeforeDragging;
30
30
CGFloat _keyboardHeightBeforeDragging;
31
+ CGRect _cachedKeyboardRect;
31
32
}
32
33
33
34
// The shared scrollView pointer, either a tableView or collectionView
@@ -85,7 +86,7 @@ - (instancetype)initWithTableViewStyle:(UITableViewStyle)style
85
86
{
86
87
NSAssert ([self class ] != [SLKTextViewController class ], @" Oops! You must subclass SLKTextViewController." );
87
88
NSAssert (style == UITableViewStylePlain || style == UITableViewStyleGrouped, @" Oops! You must pass a valid UITableViewStyle." );
88
-
89
+
89
90
if (self = [super initWithNibName: nil bundle: nil ])
90
91
{
91
92
self.scrollViewProxy = [self tableViewWithStyle: style];
@@ -98,7 +99,7 @@ - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
98
99
{
99
100
NSAssert ([self class ] != [SLKTextViewController class ], @" Oops! You must subclass SLKTextViewController." );
100
101
NSAssert ([layout isKindOfClass: [UICollectionViewLayout class ]], @" Oops! You must pass a valid UICollectionViewLayout object." );
101
-
102
+
102
103
if (self = [super initWithNibName: nil bundle: nil ])
103
104
{
104
105
self.scrollViewProxy = [self collectionViewWithLayout: layout];
@@ -111,7 +112,7 @@ - (instancetype)initWithScrollView:(UIScrollView *)scrollView
111
112
{
112
113
NSAssert ([self class ] != [SLKTextViewController class ], @" Oops! You must subclass SLKTextViewController." );
113
114
NSAssert ([scrollView isKindOfClass: [UIScrollView class ]], @" Oops! You must pass a valid UIScrollView object." );
114
-
115
+
115
116
if (self = [super initWithNibName: nil bundle: nil ])
116
117
{
117
118
_scrollView = scrollView;
@@ -127,7 +128,7 @@ - (instancetype)initWithCoder:(NSCoder *)decoder
127
128
{
128
129
NSAssert ([self class ] != [SLKTextViewController class ], @" Oops! You must subclass SLKTextViewController." );
129
130
NSAssert ([decoder isKindOfClass: [NSCoder class ]], @" Oops! You must pass a valid decoder object." );
130
-
131
+
131
132
if (self = [super initWithCoder: decoder])
132
133
{
133
134
UITableViewStyle tableViewStyle = [[self class ] tableViewStyleForCoder: decoder];
@@ -158,6 +159,8 @@ - (void)slk_commonInit
158
159
159
160
self.automaticallyAdjustsScrollViewInsets = YES ;
160
161
self.extendedLayoutIncludesOpaqueBars = YES ;
162
+
163
+ _cachedKeyboardRect = CGRectNull ;
161
164
}
162
165
163
166
@@ -241,7 +244,7 @@ - (void)viewSafeAreaInsetsDidChange
241
244
{
242
245
[super viewSafeAreaInsetsDidChange ];
243
246
244
- [self slk_updateViewConstraints ];
247
+ [self slk_updateViewConstraintsFromCachedKeyboard ];
245
248
}
246
249
247
250
@@ -266,7 +269,7 @@ - (UITableView *)tableViewWithStyle:(UITableViewStyle)style
266
269
_tableView.dataSource = self;
267
270
_tableView.delegate = self;
268
271
_tableView.clipsToBounds = NO ;
269
-
272
+
270
273
[self slk_updateInsetAdjustmentBehavior ];
271
274
}
272
275
return _tableView;
@@ -397,9 +400,9 @@ - (CGFloat)slk_appropriateKeyboardHeightFromNotification:(NSNotification *)notif
397
400
return [self slk_appropriateBottomMargin ];
398
401
}
399
402
400
- CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue ];
403
+ _cachedKeyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue ];
401
404
402
- return [self slk_appropriateKeyboardHeightFromRect: keyboardRect ];
405
+ return [self slk_appropriateKeyboardHeightFromRect: _cachedKeyboardRect ];
403
406
}
404
407
405
408
- (CGFloat )slk_appropriateKeyboardHeightFromRect : (CGRect )rect
@@ -588,16 +591,16 @@ - (void)setBounces:(BOOL)bounces
588
591
}
589
592
590
593
- (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;
599
601
}
600
602
}
603
+ }
601
604
602
605
- (BOOL )slk_updateKeyboardStatus : (SLKKeyboardStatus)status
603
606
{
@@ -906,7 +909,7 @@ - (void)setTextInputbarHidden:(BOOL)hidden animated:(BOOL)animated
906
909
}
907
910
908
911
_textInputbar.hidden = hidden;
909
-
912
+
910
913
if (@available (iOS 11.0 , *)) {
911
914
[self viewSafeAreaInsetsDidChange ];
912
915
}
@@ -1196,7 +1199,7 @@ - (void)slk_enableTypingSuggestionIfNeeded
1196
1199
BOOL enable = !self.isAutoCompleting ;
1197
1200
1198
1201
NSString *inputPrimaryLanguage = self.textView .textInputMode .primaryLanguage ;
1199
-
1202
+
1200
1203
// Toggling autocorrect on Japanese keyboards breaks autocompletion by replacing the autocompletion prefix by an empty string.
1201
1204
// So for now, let's not disable autocorrection for Japanese.
1202
1205
if ([inputPrimaryLanguage isEqualToString: @" ja-JP" ]) {
@@ -1345,7 +1348,7 @@ - (void)didPressEscapeKey:(UIKeyCommand *)keyCommand
1345
1348
else if (_textInputbar.isEditing ) {
1346
1349
[self didCancelTextEditing: keyCommand];
1347
1350
}
1348
-
1351
+
1349
1352
CGFloat bottomMargin = [self slk_appropriateBottomMargin ];
1350
1353
1351
1354
if ([self ignoreTextInputbarAdjustment ] || ([self .textView isFirstResponder ] && self.keyboardHC .constant == bottomMargin)) {
@@ -1399,6 +1402,8 @@ - (void)slk_willShowOrHideKeyboard:(NSNotification *)notification
1399
1402
// Programatically stops scrolling before updating the view constraints (to avoid scrolling glitch).
1400
1403
if (status == SLKKeyboardStatusWillShow) {
1401
1404
[self .scrollViewProxy slk_stopScrolling ];
1405
+ } else if (status == SLKKeyboardStatusWillHide) {
1406
+ _cachedKeyboardRect = CGRectNull ;
1402
1407
}
1403
1408
1404
1409
// Stores the previous keyboard height
@@ -1478,6 +1483,7 @@ - (void)slk_didShowOrHideKeyboard:(NSNotification *)notification
1478
1483
if (!self.isViewVisible ) {
1479
1484
if (status == SLKKeyboardStatusDidHide && self.keyboardStatus == SLKKeyboardStatusWillHide) {
1480
1485
// Even if the view isn't visible anymore, let's still continue to update all states.
1486
+ _cachedKeyboardRect = CGRectNull ;
1481
1487
}
1482
1488
else {
1483
1489
return ;
@@ -2287,13 +2293,25 @@ - (void)slk_updateViewConstraints
2287
2293
[super updateViewConstraints ];
2288
2294
}
2289
2295
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
+ }
2290
2308
2291
2309
#pragma mark - Keyboard Command registration
2292
2310
2293
2311
- (void )slk_registerKeyCommands
2294
2312
{
2295
2313
__weak typeof (self) weakSelf = self;
2296
-
2314
+
2297
2315
// Enter Key
2298
2316
[self .textView observeKeyInput: @" \r " modifiers: 0 title: NSLocalizedString(@" Send/Accept" , nil ) completion: ^(UIKeyCommand *keyCommand) {
2299
2317
[weakSelf didPressReturnKey: keyCommand];
@@ -2360,7 +2378,7 @@ - (void)slk_registerNotifications
2360
2378
- (void )slk_unregisterNotifications
2361
2379
{
2362
2380
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter ];
2363
-
2381
+
2364
2382
// Keyboard notifications
2365
2383
[notificationCenter removeObserver: self name: UIKeyboardWillShowNotification object: nil ];
2366
2384
[notificationCenter removeObserver: self name: UIKeyboardWillHideNotification object: nil ];
@@ -2446,3 +2464,4 @@ - (void)dealloc
2446
2464
}
2447
2465
2448
2466
@end
2467
+
0 commit comments