KeyboardAwareScrollView 有一个名为 extraScrollHeight 的道具,可用于此目的。
https://github.com/APSL/react-native-keyboard-aware-scroll-view#props
extraScrollHeight - 为键盘添加额外的偏移量。如果你想坚持的话很有用
键盘上方的元素。
您可以将它与onFocus 属性结合起来设置extraScrollHeight,以便键盘保持在按钮下方。
<KeyboardAwareScrollView
contentContainerStyle={{
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
alignItems: 'center',
backgroundColor: 'skyblue'
}}
extraScrollHeight={this.state.extraScrollHeight}>
<View>
<TextInput
ref={ref => { this.textInput = ref; }}
placeholder='John'
onFocus={(event) => {
this.setState({extraScrollHeight:30})
}}
autoFocus={true}
/>
<Button>
<Text>Submit</Text>
</Button>
</View>
</KeyboardAwareScrollView>
这将允许您根据您正在查看的TextInput 动态设置extraScrollHeight。您需要管理每个TextInput 上的extraScrollHeight。
或者,您可以设置一个extraScrollheight 并保留它。
键盘上方移动按钮更新
问题发布者更新了他们的问题,指出 TextInput 位于页面顶部,而按钮位于底部。按钮显示在键盘上方向上移动。
或者,您可以将侦听器添加到Keyboard,因为这将获得键盘的高度并允许您为按钮设置动画。
import { Keyboard, Animated } from 'react-native'
- 为按钮的初始位置设置一个新的
Animated.Value。
- 在
componentDidMount 中为keyboardDidShow 和keyboardDidHide 添加侦听器,并在componentWillUnmount 中删除它们
- 将方法添加到
_keyboardShow 和_keyboardHide,这将使按钮动画化到键盘高度以上
- 在
Animated.View 中包含按钮,位置由this.state.initialPosition 设置
代码如下:
import * as React from 'react';
import { View, StyleSheet, Animated, Button, TextInput, Keyboard } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
initialPosition: new Animated.Value(60)
}
}
componentDidMount () {
this.keyboardShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardShow);
this.keyboardHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardHide);
}
componentWillUnmount() {
this.keyboardShowListener.remove();
this.keyboardHideListener.remove();
}
_keyboardHide = (e) => {
Animated.timing(
this.state.initialPosition,
{
toValue: 60
}
).start();
}
_keyboardShow = (e) => {
Animated.timing(
this.state.initialPosition,
{
toValue: e.endCoordinates.height
}
).start();
}
render() {
return (
<View style={styles.container}>
<View style={styles.mainContainer}>
<TextInput
placeholder='Enter first name'
autoFocus
style={{fontSize: 24}}
/>
</View>
<Animated.View style={{bottom: this.state.initialPosition}}>
<Button
onPress={() => alert('submit')} title={'submit'}
/>
</Animated.View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
},
mainContainer: {
flex: 1,
alignItems: 'center'
}
});
这里是小吃https://snack.expo.io/@andypandy/animated-button-above-keyboard
值得注意的是
请注意,如果您将 android:windowSoftInputMode 设置为 adjustResize 或
adjustNothing,只有keyboardDidShow 和keyboardDidHide 事件会
在安卓上可用。 keyboardWillShow 和 keyboardWillHide 是
通常在 Android 上不可用,因为没有原生
对应事件
https://facebook.github.io/react-native/docs/keyboard#addlistener
否则我会使用keyboardWillShow 和keyboardWillHide,因为它们会在键盘显示/隐藏之前被调用,从而使动画更加流畅。
最后的想法
显然,这是一个概念证明,但它应该让您了解如何完成您想要的。
为了提高性能,您可以做的一件事是,如果您之前在应用程序中的任何位置显示键盘,则可以捕获键盘的高度并将其保存,以便以后访问。您可以将其保存在redux、AsyncStorage 中,或者只是通过导航将其传递到此屏幕。然后您可以使用TextInput 的onFocus 属性来移动按钮。