繁体   English   中英

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并在其中包含ViewForm

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

将 minHeight 属性添加到根视图样式。

<KeyboardAvoidingView 
      behavior={Platform.OS == 'ios' ? 'padding' : 'height'}
      style={styles.container}> 
        {//content } 
</KeyboardAvoidingView>



minHeight: Math.round(Dimensions.get('window').height),

不要忘记导入 Dimensions 表单 react native

import { Dimensions } from 'react-native';

如果您仍然有其他问题,还有另一个很酷的库可以执行此操作。.https ://www.npmjs.com/package/react-native-keyboard-aware-scroll-view

<KeyboardAwareScrollView>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

对于在 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,目标元素将不会相应地调整其高度以避免键盘

我可以通过这个实现实现正确的行为:

  1. 删除了 KeyboardAvoidingView 组件。
  2. 将此添加到app.json文件中:
     "android": {
      "softwareKeyboardLayoutMode": "pan"
    }

希望它也适用于你们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM