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 #37766

Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6cd100a
Automatic keyboard insets improvements
isidoro98 Nov 6, 2022
24ce8b5
Prevent unintended scrolls on native navigation
isidoro98 Nov 17, 2022
7883315
Add bottomKeyboardOffset prop
isidoro98 Nov 17, 2022
42088b6
formatting
isidoro98 Nov 17, 2022
e2891a7
Update RCTScrollView.m
isidoro98 Nov 17, 2022
7422c3c
Merge branch 'main' into feature/automaticKeyboardInsetsImprovements
adamaveray Jun 8, 2023
4b9e418
Add ScrollView keyboard insets example to RNTester
adamaveray Jun 8, 2023
ee6bbc3
Remove scroll view bottom keyboard offset property
adamaveray Jun 15, 2023
12376c5
Add multiline inputs to keyboard insets tester
adamaveray Jun 15, 2023
915be16
Add external text input to keyboard insets tester
adamaveray Jun 15, 2023
145cc56
Improve scroll view keyboard insets text handling
adamaveray Jun 15, 2023
9d7205f
Merge branch 'main' into feature/automaticKeyboardInsetsImprovements
adamaveray Jun 16, 2023
08078a4
Prevent scrolling when focusing external textfield
adamaveray Jun 16, 2023
62fc88a
Handle textfield focus in inverted scroll views
adamaveray Jun 16, 2023
5e895f2
Support FlatLists in keyboard insets tester
adamaveray Jun 16, 2023
8798130
Apply requested code format changes
adamaveray Jun 19, 2023
ca5ead8
Improve native text field keyboard offsetting
adamaveray Jun 19, 2023
669dcff
Simplify calculating text input focus area
adamaveray Jul 14, 2023
cf08565
Document new scroll view property
adamaveray Jul 14, 2023
f643553
Add scroll view keyboard inset height test option
adamaveray Jul 14, 2023
b64e7a3
Merge branch 'main' into feature/automaticKeyboardInsetsImprovements
adamaveray Sep 13, 2023
c2eab2c
Rename iOS-specific example implementation
adamaveray Sep 13, 2023
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 @@ -200,6 +200,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
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 packages/react-native/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;
}
}

if (@available(iOS 14.0, *)) {
Expand Down
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 packages/react-native/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 packages/react-native/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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.
*/

'use strict';

import * as React from 'react';

import {
ScrollView,
StyleSheet,
Switch,
Text,
TextInput,
View,
} from 'react-native';

export function ScrollViewKeyboardInsetsExample() {
const [automaticallyAdjustKeyboardInsets, setAutomaticallyAdjustKeyboardInsets] = React.useState(true);
const [bottomKeyboardOffset, setBottomKeyboardOffset] = React.useState(0);

return (
<View style={styles.container}>
<View style={styles.controlRow}>
<Text><Text style={styles.code}>automaticallyAdjustKeyboardInsets</Text> is {automaticallyAdjustKeyboardInsets + ''}</Text>
<Switch
onValueChange={v => setAutomaticallyAdjustKeyboardInsets(v)}
value={automaticallyAdjustKeyboardInsets}
style={styles.controlSwitch}/>
</View>
<View style={styles.controlRow}>
<Text style={styles.code}>bottomKeyboardOffset</Text>
<TextInput
placeholder={bottomKeyboardOffset + ''}
keyboardType={'numeric'}
onChangeText={v => setBottomKeyboardOffset(parseInt(v || '0', 10))}
style={styles.controlTextInput}/>
</View>
<ScrollView
contentContainerStyle={[
styles.scrollViewContent,
]}
automaticallyAdjustKeyboardInsets={automaticallyAdjustKeyboardInsets}
bottomKeyboardOffset={bottomKeyboardOffset}
keyboardDismissMode={'interactive'}>
{[...Array(20).keys()].map(item => (
<View key={item} style={styles.textInputRow}>
<TextInput placeholder={item.toString()} style={styles.textInput}/>
</View>
))}
</ScrollView>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'flex-start',
},
scrollViewContent: {
paddingVertical: 5,
paddingHorizontal: 10,
},
textInputRow: {
borderWidth: 1,
marginVertical: 8,
borderColor: '#999',
},
textInput: {
width: '100%',
backgroundColor: '#fff',
fontSize: 24,
padding: 8,
},
controlRow: {
padding: 10,
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
backgroundColor: '#fff',
borderTopWidth: 1,
borderTopColor: '#ccc',
borderBottomWidth: 1,
borderBottomColor: '#ccc',
},
controlSwitch: {
},
controlTextInput: {
flex: 1,
paddingVertical: 10,
paddingHorizontal: 10,
marginVertical: -10,
marginRight: -10,
fontSize: 20,
textAlign: 'right',
},
code: {
fontSize: 12,
fontFamily: 'Courier',
},
});

exports.title = 'ScrollViewKeyboardInsets';
exports.category = 'iOS';
exports.description =
'ScrollView automaticallyAdjustKeyboardInsets adjusts keyboard insets when soft keyboard is activated.';
exports.examples = [
{
title: '<ScrollView> automaticallyAdjustKeyboardInsets Example',
render: (): React.Node => <ScrollViewKeyboardInsetsExample/>,
},
];
4 changes: 4 additions & 0 deletions packages/rn-tester/js/utils/RNTesterList.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const Components: Array<RNTesterModuleInfo> = [
key: 'ScrollViewIndicatorInsetsExample',
module: require('../examples/ScrollView/ScrollViewIndicatorInsetsExample'),
},
{
key: 'ScrollViewKeyboardInsetsExample',
module: require('../examples/ScrollView/ScrollViewKeyboardInsetsExample'),
},
{
key: 'SectionListIndex',
module: require('../examples/SectionList/SectionListIndex'),
Expand Down