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

Feature: Improvements to automaticallyAdjustKeyboardInsets #35224

Closed
wants to merge 5 commits into from
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
1 change: 1 addition & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ REACT_PUBLIC_HEADERS = {
"React/RCTViewUtils.h": RCTVIEWS_PATH + "RCTViewUtils.h",
"React/RCTWeakProxy.h": RCTBASE_PATH + "RCTWeakProxy.h",
"React/RCTWrapperViewController.h": RCTVIEWS_PATH + "RCTWrapperViewController.h",
"React/UIResponder+FirstResponder.h": RCTVIEWS_PATH + "UIResponder+FirstResponder.h",
"React/UIView+React.h": RCTVIEWS_PATH + "UIView+React.h",
}

Expand Down
6 changes: 6 additions & 0 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ type IOSProps = $ReadOnly<{|
* @platform ios
*/
contentInset?: ?EdgeInsetsProp,
/**
* Controls the distance between the soft keyboard and the TextInput
* when using `automaticallyAdjustKeyboardInsets`.
* @platform ios
*/
bottomKeyboardOffset?: ?number,
/**
* When true, the scroll view bounces when it reaches the end of the
* content if the content is larger then the scroll view along the axis of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ScrollViewNativeProps = $ReadOnly<{
automaticallyAdjustContentInsets?: ?boolean,
automaticallyAdjustKeyboardInsets?: ?boolean,
automaticallyAdjustsScrollIndicatorInsets?: ?boolean,
bottomKeyboardOffset?: ?number,
bounces?: ?boolean,
bouncesZoom?: ?boolean,
canCancelContentTouches?: ?boolean,
Expand Down
1 change: 1 addition & 0 deletions React/Views/ScrollView/RCTScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@property (nonatomic, assign) UIEdgeInsets contentInset;
@property (nonatomic, assign) BOOL automaticallyAdjustContentInsets;
@property (nonatomic, assign) BOOL automaticallyAdjustKeyboardInsets;
@property (nonatomic, assign) CGFloat bottomKeyboardOffset;
@property (nonatomic, assign) BOOL DEPRECATED_sendUpdatedChildFrames;
@property (nonatomic, assign) NSTimeInterval scrollEventThrottle;
@property (nonatomic, assign) BOOL centerContent;
Expand Down
25 changes: 21 additions & 4 deletions React/Views/ScrollView/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "RCTViewUtils.h"
#import "UIView+Private.h"
#import "UIView+React.h"
#import "UIResponder+FirstResponder.h"

/**
* Include a custom scroll view subclass because we want to limit certain
Expand Down Expand Up @@ -307,6 +308,7 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
}

double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

UIViewAnimationCurve curve =
(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
CGRect beginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
Expand All @@ -324,11 +326,26 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
}

CGPoint newContentOffset = _scrollView.contentOffset;
CGFloat contentDiff = endFrame.origin.y - beginFrame.origin.y;
if (self.inverted) {
newContentOffset.y += contentDiff;
UIResponder *firstResponder = [UIResponder currentFirstResponder];
if ([firstResponder isKindOfClass: [UITextField class]] && [(UITextField *) firstResponder isDescendantOfView:_scrollView]) {
UITextField *textField = [UIResponder currentFirstResponder];
CGRect textFieldFrame = [textField.superview convertRect:textField.frame toView:nil];
CGFloat textFieldBottom = textFieldFrame.origin.y + textFieldFrame.size.height + _bottomKeyboardOffset;
CGFloat contentDiff = textFieldBottom - endFrame.origin.y;
if (textFieldBottom > endFrame.origin.y && endFrame.origin.y < beginFrame.origin.y) {
if (self.inverted) {
newContentOffset.y -= contentDiff;
} else {
newContentOffset.y += contentDiff;
}
}
} else {
newContentOffset.y -= contentDiff;
CGFloat contentDiff = endFrame.origin.y - beginFrame.origin.y;
if (self.inverted) {
newContentOffset.y += contentDiff;
} else {
newContentOffset.y -= contentDiff;
}
}

[UIView animateWithDuration:duration
Expand Down
1 change: 1 addition & 0 deletions React/Views/ScrollView/RCTScrollViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(maintainVisibleContentPosition, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustKeyboardInsets, BOOL)
RCT_EXPORT_VIEW_PROPERTY(bottomKeyboardOffset, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(decelerationRate, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(directionalLockEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(indicatorStyle, UIScrollViewIndicatorStyle)
Expand Down
13 changes: 13 additions & 0 deletions React/Views/UIResponder+FirstResponder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


#import <UIKit/UIKit.h>

@interface UIResponder (FirstResponder)
+(id)currentFirstResponder;
@end
24 changes: 24 additions & 0 deletions React/Views/UIResponder+FirstResponder.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "UIResponder+FirstResponder.h"

static __weak id currentFirstResponder;

@implementation UIResponder (FirstResponder)

+(id)currentFirstResponder {
currentFirstResponder = nil;
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
return currentFirstResponder;
}

-(void)findFirstResponder:(id)sender {
currentFirstResponder = self;
}

@end