@@ -148,7 +148,7 @@ - (void)viewDidLoad
148
148
[self configureSuggestionsTableView ];
149
149
[self configureKeyboardGestureRecognizer ];
150
150
[self configureViewConstraints ];
151
- [self configureKeyboardManager ];
151
+ [self configureKeyboardObservers ];
152
152
}
153
153
154
154
- (void )viewWillAppear : (BOOL )animated
@@ -195,6 +195,10 @@ - (void)viewWillDisappear:(BOOL)animated
195
195
[[NSNotificationCenter defaultCenter ] removeObserver: self name: UIApplicationDidBecomeActiveNotification object: nil ];
196
196
}
197
197
198
+ - (void )dealloc {
199
+ [[NSNotificationCenter defaultCenter ] removeObserver: self ];
200
+ }
201
+
198
202
- (void )viewWillTransitionToSize : (CGSize )size withTransitionCoordinator : (id <UIViewControllerTransitionCoordinator>)coordinator
199
203
{
200
204
[super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
@@ -359,26 +363,57 @@ - (void)configureKeyboardGestureRecognizer
359
363
[self .view addGestureRecognizer: self .tapOffKeyboardGesture];
360
364
}
361
365
362
- - (void )configureKeyboardManager
366
+ - (void )configureKeyboardObservers
363
367
{
364
- self.keyboardManager = [[KeyboardDismissHelper alloc ] initWithParentView: self .view
365
- scrollView: self .tableView
366
- dismissableControl: self .replyTextView
367
- bottomLayoutConstraint: self .replyTextViewBottomConstraint];
368
368
369
+ [[NSNotificationCenter defaultCenter ] addObserver: self
370
+ selector: @selector (keyboardWillShow: )
371
+ name: UIKeyboardWillShowNotification
372
+ object: nil ];
373
+ [[NSNotificationCenter defaultCenter ] addObserver: self
374
+ selector: @selector (keyboardWillHide: )
375
+ name: UIKeyboardWillHideNotification
376
+ object: nil ];
377
+ }
378
+
379
+ - (void )keyboardWillShow : (NSNotification *)notification {
380
+ __weak ReaderCommentsViewController *weakSelf = self;
381
+ [weakSelf refreshNoResultsView ];
382
+
383
+ CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue ];
384
+ CGFloat keyboardHeight = CGRectGetHeight (keyboardFrame);
385
+ CGFloat tabBarHeight = self.tabBarController .tabBar .frame .size .height ;
386
+
387
+ // Calculate the bottom inset, subtracting the tab bar height if the tab bar is not hidden
388
+ CGFloat bottomInset = keyboardHeight - (self.tabBarController .tabBar .isHidden ? 0 : tabBarHeight);
389
+
390
+ [self adjustReplyTextViewBottomConstraint: bottomInset];
391
+ }
392
+
393
+ - (void )keyboardWillHide : (NSNotification *)notification {
369
394
__weak UITableView *weakTableView = self.tableView ;
370
395
__weak ReaderCommentsViewController *weakSelf = self;
371
- self.keyboardManager .onWillHide = ^{
372
- [weakTableView deselectSelectedRowWithAnimation: YES ];
373
- [weakSelf refreshNoResultsView ];
374
- };
375
- self.keyboardManager .onWillShow = ^{
376
- [weakSelf refreshNoResultsView ];
377
- };
396
+ [weakTableView deselectSelectedRowWithAnimation: YES ];
397
+ [weakSelf refreshNoResultsView ];
398
+
399
+ [self adjustReplyTextViewBottomConstraint: self .view.safeAreaInsets.bottom];
378
400
}
379
401
380
402
#pragma mark - Autolayout Helpers
381
403
404
+ - (void )adjustReplyTextViewBottomConstraint : (CGFloat )bottomInset {
405
+ if (self.replyTextViewBottomConstraint ) {
406
+ [self .view removeConstraint: self .replyTextViewBottomConstraint];
407
+ }
408
+
409
+ self.replyTextViewBottomConstraint = [self .replyTextView.bottomAnchor constraintEqualToAnchor: self .view.bottomAnchor constant: -bottomInset];
410
+ [self .view addConstraint: self .replyTextViewBottomConstraint];
411
+
412
+ [UIView animateWithDuration: 0.3 animations: ^{
413
+ [self .view layoutIfNeeded ];
414
+ }];
415
+ }
416
+
382
417
- (void )configureViewConstraints
383
418
{
384
419
NSMutableDictionary *views = [[NSMutableDictionary alloc ] initWithDictionary: @{
@@ -405,13 +440,7 @@ - (void)configureViewConstraints
405
440
[[self .replyTextView.leftAnchor constraintEqualToAnchor: self .tableView.leftAnchor] setActive: YES ];
406
441
[[self .replyTextView.rightAnchor constraintEqualToAnchor: self .tableView.rightAnchor] setActive: YES ];
407
442
408
- self.replyTextViewBottomConstraint = [NSLayoutConstraint constraintWithItem: self .view
409
- attribute: NSLayoutAttributeBottom
410
- relatedBy: NSLayoutRelationEqual
411
- toItem: self .replyTextView
412
- attribute: NSLayoutAttributeBottom
413
- multiplier: 1.0
414
- constant: 0.0 ];
443
+ self.replyTextViewBottomConstraint = [self .replyTextView.bottomAnchor constraintEqualToAnchor: self .view.safeAreaLayoutGuide.bottomAnchor];
415
444
self.replyTextViewBottomConstraint .priority = UILayoutPriorityDefaultHigh;
416
445
417
446
[self .view addConstraint: self .replyTextViewBottomConstraint];
0 commit comments