Skip to content

Commit c8a3344

Browse files
committed
Respect safe area for "Reply to post" section on the Comments screen
1 parent 31362ef commit c8a3344

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

Diff for: WordPress/Classes/ViewRelated/Reader/Comments/ReaderCommentsViewController.m

+49-20
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ - (void)viewDidLoad
148148
[self configureSuggestionsTableView];
149149
[self configureKeyboardGestureRecognizer];
150150
[self configureViewConstraints];
151-
[self configureKeyboardManager];
151+
[self configureKeyboardObservers];
152152
}
153153

154154
- (void)viewWillAppear:(BOOL)animated
@@ -195,6 +195,10 @@ - (void)viewWillDisappear:(BOOL)animated
195195
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
196196
}
197197

198+
- (void)dealloc {
199+
[[NSNotificationCenter defaultCenter] removeObserver:self];
200+
}
201+
198202
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
199203
{
200204
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
@@ -359,26 +363,57 @@ - (void)configureKeyboardGestureRecognizer
359363
[self.view addGestureRecognizer:self.tapOffKeyboardGesture];
360364
}
361365

362-
- (void)configureKeyboardManager
366+
- (void)configureKeyboardObservers
363367
{
364-
self.keyboardManager = [[KeyboardDismissHelper alloc] initWithParentView:self.view
365-
scrollView:self.tableView
366-
dismissableControl:self.replyTextView
367-
bottomLayoutConstraint:self.replyTextViewBottomConstraint];
368368

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 {
369394
__weak UITableView *weakTableView = self.tableView;
370395
__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];
378400
}
379401

380402
#pragma mark - Autolayout Helpers
381403

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+
382417
- (void)configureViewConstraints
383418
{
384419
NSMutableDictionary *views = [[NSMutableDictionary alloc] initWithDictionary:@{
@@ -405,13 +440,7 @@ - (void)configureViewConstraints
405440
[[self.replyTextView.leftAnchor constraintEqualToAnchor:self.tableView.leftAnchor] setActive:YES];
406441
[[self.replyTextView.rightAnchor constraintEqualToAnchor:self.tableView.rightAnchor] setActive:YES];
407442

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];
415444
self.replyTextViewBottomConstraint.priority = UILayoutPriorityDefaultHigh;
416445

417446
[self.view addConstraint:self.replyTextViewBottomConstraint];

0 commit comments

Comments
 (0)