KeyboardAvoidingView 不适用于 Expo
KeyboardAvoidingView not working with Expo
在我的 React-Native Expo 应用程序中,我似乎无法使用键盘来推送内容。我正在通过从我的开发机器发布它来从 expo App 测试它。
尽我所能,当键盘进入焦点时,似乎没有任何东西可以提升视图,是否有特定的组件顺序,或者我遗漏了一些 属性;我已经包含了我认为相关的版本、渲染块和样式块。
我正在使用以下版本(最新);
"expo": "^29.0.0",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
对于登录页面,呈现代码如下所示;
render() {
return (
<SafeAreaView style={styles.flexContainer}>
<KeyboardAvoidingView
style={styles.flexContainer}
behavior="padding"
>
<Image style={styles.image} source={require('../../assets/images/backgroundWelcome.png')} role="presentation" />
<View style={styles.container}>
<View style={[styles.row, styles.border]}>
<TextInput
placeholder='Email'
style={styles.input}
onChangeText={(input) => this.setState({email: input})}
value={this.state.email}
/>
</View>
<View style={[styles.row, styles.border]}>
<TextInput
placeholder='Password'
style={styles.input}
secureTextEntry={true}
onChangeText={(input) => this.setState({password: input})}
value={this.state.password}
/>
</View>
<View style={styles.row}>
<StyledButton callback={this.handleLogin} title='Log In'/>
</View>
</View>
</KeyboardAvoidingView>
</SafeAreaView>
)
}
这些是相关的样式;
root: {
flex: 1,
alignItems: 'center',
backgroundColor: '#fff',
},
flexContainer: {
flex: 1,
},
container: {
paddingHorizontal: 40,
paddingBottom: 22,
alignItems: 'center',
flex: -1
},
row: {
flexDirection: 'row',
marginVertical: 10,
},
我最终没能直接找到上面的完整解决方案,但我确实找到了一个名为的 npm 模块。
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
然后我将其嵌套在 ScrollView
中,并在其中包含 View
和 Form
。
<ScrollView
<KeyboardAwareScrollView>
<View>
<!-- stuff -->
<View
<KeyboardAwareScrollView>
<ScrollView>
模块可以在这里找到;
react-native-keyboard-aware-scroll-view
在撰写本文时出现了一个非常受欢迎的模块,每周下载量约为 30k。我与这个模块没有任何关系,但它对我有用。
我遇到了同样的问题,将 flex: 1 添加到 KeyboardAvoidingView 解决了我的问题:
<KeyboardAvoidingView style={{ flex: 1 }} behavior={"padding"} >
<ScrollView >
<Card>
<CardItem header bordered>
<Text>Personal Information</Text>
</CardItem>
<CardItem bordered>
<Body>
<Item floatingLabel style={styles.item}>
<Label>Full Name</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Headline</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Location</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Email</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
</Body>
</CardItem>
</Card>
<ListItem>
<Left>
<Text>Make my Profile Public</Text>
</Left>
<Right>
<Switch value={this.state.publicProfileRadioValue}
onValueChange={(value) => this.setState({ publicProfileRadioValue: value })}
/>
</Right>
</ListItem>
</ScrollView>
</KeyboardAvoidingView>
对于在 Android 上测试时 运行 参与其中的任何其他人,请确保您要避免使用键盘输入的元素也具有 flex: 1
。
这行不通:
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<TextInput style={{ height: 300 }} />
</KeyboardAvoidingView>
这将:
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<TextInput style={{ flex: 1 }} />
</KeyboardAvoidingView>
如果没有 flex 1,目标元素将不会相应地调整其高度以避免键盘
我可以通过此实现实现正确的行为:
- 删除了 KeyboardAvoidingView 组件。
- 将此添加到
app.json
文件:
"android": {
"softwareKeyboardLayoutMode": "pan"
}
希望对你们也有用!
将 minHeight 属性 添加到根视图样式。
<KeyboardAvoidingView
behavior={Platform.OS == 'ios' ? 'padding' : 'height'}
style={styles.container}>
{//content }
</KeyboardAvoidingView>
minHeight: Math.round(Dimensions.get('window').height),
不要忘记从 react native 导入 Dimensions
import { Dimensions } from 'react-native';
在我的 React-Native Expo 应用程序中,我似乎无法使用键盘来推送内容。我正在通过从我的开发机器发布它来从 expo App 测试它。
尽我所能,当键盘进入焦点时,似乎没有任何东西可以提升视图,是否有特定的组件顺序,或者我遗漏了一些 属性;我已经包含了我认为相关的版本、渲染块和样式块。
我正在使用以下版本(最新);
"expo": "^29.0.0",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
对于登录页面,呈现代码如下所示;
render() {
return (
<SafeAreaView style={styles.flexContainer}>
<KeyboardAvoidingView
style={styles.flexContainer}
behavior="padding"
>
<Image style={styles.image} source={require('../../assets/images/backgroundWelcome.png')} role="presentation" />
<View style={styles.container}>
<View style={[styles.row, styles.border]}>
<TextInput
placeholder='Email'
style={styles.input}
onChangeText={(input) => this.setState({email: input})}
value={this.state.email}
/>
</View>
<View style={[styles.row, styles.border]}>
<TextInput
placeholder='Password'
style={styles.input}
secureTextEntry={true}
onChangeText={(input) => this.setState({password: input})}
value={this.state.password}
/>
</View>
<View style={styles.row}>
<StyledButton callback={this.handleLogin} title='Log In'/>
</View>
</View>
</KeyboardAvoidingView>
</SafeAreaView>
)
}
这些是相关的样式;
root: {
flex: 1,
alignItems: 'center',
backgroundColor: '#fff',
},
flexContainer: {
flex: 1,
},
container: {
paddingHorizontal: 40,
paddingBottom: 22,
alignItems: 'center',
flex: -1
},
row: {
flexDirection: 'row',
marginVertical: 10,
},
我最终没能直接找到上面的完整解决方案,但我确实找到了一个名为的 npm 模块。
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
然后我将其嵌套在 ScrollView
中,并在其中包含 View
和 Form
。
<ScrollView
<KeyboardAwareScrollView>
<View>
<!-- stuff -->
<View
<KeyboardAwareScrollView>
<ScrollView>
模块可以在这里找到;
react-native-keyboard-aware-scroll-view
在撰写本文时出现了一个非常受欢迎的模块,每周下载量约为 30k。我与这个模块没有任何关系,但它对我有用。
我遇到了同样的问题,将 flex: 1 添加到 KeyboardAvoidingView 解决了我的问题:
<KeyboardAvoidingView style={{ flex: 1 }} behavior={"padding"} >
<ScrollView >
<Card>
<CardItem header bordered>
<Text>Personal Information</Text>
</CardItem>
<CardItem bordered>
<Body>
<Item floatingLabel style={styles.item}>
<Label>Full Name</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Headline</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Location</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Email</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
<Item floatingLabel style={styles.item}>
<Label>Phone Number</Label>
<Input />
</Item>
</Body>
</CardItem>
</Card>
<ListItem>
<Left>
<Text>Make my Profile Public</Text>
</Left>
<Right>
<Switch value={this.state.publicProfileRadioValue}
onValueChange={(value) => this.setState({ publicProfileRadioValue: value })}
/>
</Right>
</ListItem>
</ScrollView>
</KeyboardAvoidingView>
对于在 Android 上测试时 运行 参与其中的任何其他人,请确保您要避免使用键盘输入的元素也具有 flex: 1
。
这行不通:
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<TextInput style={{ height: 300 }} />
</KeyboardAvoidingView>
这将:
<KeyboardAvoidingView
behavior={Platform.OS == "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<TextInput style={{ flex: 1 }} />
</KeyboardAvoidingView>
如果没有 flex 1,目标元素将不会相应地调整其高度以避免键盘
我可以通过此实现实现正确的行为:
- 删除了 KeyboardAvoidingView 组件。
- 将此添加到
app.json
文件:
"android": {
"softwareKeyboardLayoutMode": "pan"
}
希望对你们也有用!
将 minHeight 属性 添加到根视图样式。
<KeyboardAvoidingView
behavior={Platform.OS == 'ios' ? 'padding' : 'height'}
style={styles.container}>
{//content }
</KeyboardAvoidingView>
minHeight: Math.round(Dimensions.get('window').height),
不要忘记从 react native 导入 Dimensions
import { Dimensions } from 'react-native';