Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] automaticallyAdjustKeyboardInsets not shifting scrollview content #46732

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ - (void)reactUpdateResponderOffsetForScrollView:(RCTScrollView *)scrollView
{
if (![self isDescendantOfView:scrollView]) {
// View is outside scroll view
scrollView.firstResponderViewOutsideScrollView = self.backedTextInputView;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ NS_ASSUME_NONNULL_BEGIN
/** Focus area of newly-activated text input relative to the window to compare against UIKeyboardFrameBegin/End */
@property (nonatomic, assign) CGRect firstResponderFocus;

/** newly-activated text input outside of the scroll view */
@property (nonatomic, weak) UIView *firstResponderViewOutsideScrollView;

/*
* Returns the subview of the scroll view that the component uses to mount all subcomponents into. That's useful to
* separate component views from auxiliary views to be able to reliably implement pull-to-refresh- and RTL-related
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
UIViewAnimationCurve curve =
(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
CGRect keyboardEndFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardBeginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];

CGPoint absoluteViewOrigin = [self convertPoint:self.bounds.origin toView:nil];
CGFloat scrollViewLowerY = isInverted ? absoluteViewOrigin.y : absoluteViewOrigin.y + self.bounds.size.height;
Expand All @@ -203,21 +204,24 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
from:self
forEvent:nil]) {
if (CGRectEqualToRect(_firstResponderFocus, CGRectNull)) {
// Text input view is outside of the scroll view.
return;
}

CGRect viewIntersection = CGRectIntersection(self.firstResponderFocus, keyboardEndFrame);
UIView *inputAccessoryView = _firstResponderViewOutsideScrollView.inputAccessoryView;
if (inputAccessoryView) {
// Text input view is within the inputAccessoryView.
contentDiff = keyboardEndFrame.origin.y - keyboardBeginFrame.origin.y;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want an early return here? Or should we have a general check for contentDiff == 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to maintain the same logic as the old architecture, which does not perform early return checks or check for contentDiff == 0.

} else {
CGRect viewIntersection = CGRectIntersection(self.firstResponderFocus, keyboardEndFrame);

if (CGRectIsNull(viewIntersection)) {
return;
}
if (CGRectIsNull(viewIntersection)) {
return;
}

// Inner text field focused
CGFloat focusEnd = CGRectGetMaxY(self.firstResponderFocus);
if (focusEnd > keyboardEndFrame.origin.y) {
// Text field active region is below visible area with keyboard - update diff to bring into view
contentDiff = keyboardEndFrame.origin.y - focusEnd;
// Inner text field focused
CGFloat focusEnd = CGRectGetMaxY(self.firstResponderFocus);
if (focusEnd > keyboardEndFrame.origin.y) {
// Text field active region is below visible area with keyboard - update diff to bring into view
contentDiff = keyboardEndFrame.origin.y - focusEnd;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ - (void)reactUpdateResponderOffsetForScrollView:(RCTScrollViewComponentView *)sc
{
if (![self isDescendantOfView:scrollView.scrollView] || !_backedTextInputView.isFirstResponder) {
// View is outside scroll view or it's not a first responder.
scrollView.firstResponderViewOutsideScrollView = _backedTextInputView;
return;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/React/Views/ScrollView/RCTScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
@property (nonatomic, assign) BOOL inverted;
/** Focus area of newly-activated text input relative to the window to compare against UIKeyboardFrameBegin/End */
@property (nonatomic, assign) CGRect firstResponderFocus;
/** newly-activated text input outside of the scroll view */
@property (nonatomic, weak) UIView *firstResponderViewOutsideScrollView;

// NOTE: currently these event props are only declared so we can export the
// event names to JS - we don't call the blocks directly because scroll events
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/React/Views/ScrollView/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
if (!didFocusExternalTextField && focusEnd > endFrame.origin.y) {
// Text field active region is below visible area with keyboard - update diff to bring into view
contentDiff = endFrame.origin.y - focusEnd;
} else {
UIView *inputAccessoryView = _firstResponderViewOutsideScrollView.inputAccessoryView;
if (inputAccessoryView) {
// Text input view is within the inputAccessoryView.
contentDiff = endFrame.origin.y - beginFrame.origin.y;
}
zhongwuzw marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (endFrame.origin.y <= beginFrame.origin.y) {
// Keyboard opened for other reason
Expand Down
Loading